QStudio
Query kdb+ Servers and Chart Results.
It is possible to import C functions directly for use within the kdb process by dynamically loading modules. Below we will look at an example of compiling, importing and executing C functions on windows for kdb 3.0+.
We will be adding two functions myavg, mysum
which provide
moving average and moving sum functions respectively. Yes, kdb does already mavg but read till the end and I
think you will find the performance comparison interesting. The Interfacing Kdb with C and
Extending with C page on the code KX website documents the C API extensively,
we will give details for the core parts used in our example but you will want to read those pages thoroughly at a later stage.
The steps to compile and load a C DLL are as follows:
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64
cl /LD /DKXVER=3 mymoving.c mymoving.def q.lib
mytop, myavg
functions.In this q script we import functions from our DLL and assign them to variable names:
mymoving
is the name of our DLL which is in the current directory, you could place this in c:/q/w64
or specify a full path.
2:
is the kdb function for loading native code.
The right hand side e.g. (`mysum;2)
specifies a function that exists and the number of arguments it accepts.
As you can see we get the same results using either our own function or kdb's builtin mavg. However our function is 46x quicker (users on linux report a much smaller 3x speed difference). There are a number of reasons why this is (nulls and partitioned data) but that's a topic for another post.
If your having problems compiling or want advice either contact us or ask in our forums. We also provide kdb consulting services and have particular expertise in writing kdb+tick subscribers and feedhandlers.
Query kdb+ Servers and Chart Results.