« What environment variables do I need to know about when kdb starts up? | Main | I defined an untyped list, but I get a type error when I add more than one element to it? How do I store mixed data in a list? »

I know k sits under q. By porting q code to k, will it run faster?

No. The interpreter boils it down to the same bytecode.

Consider Problem 1 from Project Euler: sum the multiples of 3 and 5 which are less than 1000. You may want to try this before reading further. The problems from Project Euler are great exercises for learning q!

The code below shows one solution written in both languages; f is written in q, while g is written in k.

q)f: {(+/) distinct where (0 = x mod 3) | 0 = x mod 5}
q)\
  g: {+/?&(0={x-y*x div y}[x;3])|0={x-y*x div y}[x;5]}
  \
q)

You can see the bytecode for a function (as well as some generally more useful information, like the function's arguments) by calling value on it:

q)value f
0xa278a10a020c48a078a10a020c48462531a31b520004
,`x
`symbol$()
,`
3
k){x-y*x div y}
5
+
"{(+/) distinct where (0 = x mod 3) | 0 = x mod 5}"
q)

As you can see, when passed a function, value returns a list. The first element of that list is the function's bytecode. Now it is easy to show that f and g have the same bytecode:

q)first value f
0xa278a10a020c48a078a10a020c48462531a31b520004
q)first value g
0xa278a10a020c48a078a10a020c48462531a31b520004
q)all (first value f) = first value g
1b
q)

In q, however, you probably would have written the solution a tiny bit differently:

q)f: {sum distinct where (0 = x mod 3) | 0 = x mod 5}
q)

This results in a q version that is actually two bytes smaller and (maybe) a hair faster:

q)first value f
0xa278a10a020c48a078a10a020c48462531390004
q)first value g
0xa278a10a020c48a078a10a020c48462531a31b520004
q)\t do[100000; f til 1000]
3240
q)\t do[100000; g til 1000]
3253
q)

Thanks to Charlie Skelton from kx for pointing out errors in the original version of this article.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Textile formatting is allowed.