Search code examples
kdb

Is it performant to use floor function instead of lower function to convert string/symbol to lower case in kdb


Since both functions, lower(with certain condition) and floor uses same k operator internally, so I tried using floor function instead of lower function to convert string/symbol to lower case, and the test results were illuminating.
Test results clearly showed that floor is more performant compared to lower.

q)\t:10 floor 100000 4#.Q.A
61
q)\t:10 lower 100000 4#.Q.A
941
q)\t:100 lower 100000 4#.Q.A
9421
q)\t:100 floor 100000 4#.Q.A
568
q)\t:10 floor 100000 4#`$'.Q.A
170
q)\t:10 lower 100000 4#`$'.Q.A
1063

Practically, is it beneficial/optimum to use floor function instead lower function to convert string/symbol to lower case?


Solution

  • lower covers more cases/scenarios than floor so it's a better all-rounder. For example, floor does not work on enumerations

    q)e:`s?`AbC`DEf`gh`I
    q)e
    `s$`AbC`DEf`gh`I
    q)type e
    20h
    q)lower e
    `abc`def`gh`i
    q)floor e
    'type
      [0]  floor e
           ^
    

    So you can't use floor on historical/on-disk symbols. However if you know that you'd only be using it on in-memory data then yes, floor would be quicker as it doesn't require all the checks that are required by lower