Often I get asked:

  • How can I time how long to run a query?
  • Does kdb have a profiler?
  • How can I tell which query is faster?
The answer to all these is \ts as we'll look at below.

Timing a Query

First let's create a table t.

q)t:([] a:til n; s:n?`3; ex:n?4?`3; size:n?100; price:n?1000.)
 1
2
3
4
5
6
 
q)n:1200300
q)t:([] a:til n; s:n?`3; ex:n?4?`3; size:n?100; price:n?1000.)
q)t
a s   ex  size price
-----------------------
0 ddd ifb 37   537.5576

To time how long a query takes to run we could use the system t or \t command. Running a command prefixed with \t causes it to return the time that it took to run in milliseconds. We can then go one step further and use the \t:n command to run the same query n times and return the total time in milliseconds. As shown below:

q)/ 3 milliseconds is too quick to really know. Run it 100 times
 1
2
3
4
5
6
 
q)select from t where s=`ddd
a     s   ex  size price
---------------------------
0     ddd ifb 37   537.5576
443   ddd ifb 37   383.565
1154  ddd peg 12   385.8024

Finding the memory required for a Query

If we want to profile both how much time and how much memory a query takes to run we can use \ts:n. This returns two values, the time taken in milliseconds and the space required in bytes.

q)\ts:100 select from t where s=`ddd
1
2
3
4
5
 
q)\ts select from t where s=`ddd
4 2101600
q)\ts:100 select from t where s=`ddd
298 2101536		
        

One area that we have used this is as part of the qunit test framework for kdb, we log how much memory and time a test takes to run. The framework also allows specifying the maximum time/memory that a unit test should take.

Another interesting idea to try is automatically breaking functions into statements and timing each part, or creating a tool that profiles each line. QStudio provides a basic version of this:

vbasic kdb language profiler