mchen
mchen

Reputation: 10156

Efficient way of computing running count of "how many x less than y?" in KDB

I have 2 sorted lists x and y and I want to find a running count of how many x are less than y. The following works for now:

x:10*til 3;   / 0 10 20
y:til 30;     / 0 1 2 ... 30
sum {x < y}[;y] each x
q) 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

but I suspect using each isn't the most efficient way of doing things. Considering my x,y may end up being in the order of tens of millions of items, is there a faster way?

Upvotes: 1

Views: 255

Answers (1)

WooiKent Lee
WooiKent Lee

Reputation: 1311

You can consider using slaves to perform parallel execution if the function gets too expensive. My example uses 8 slaves.

>q -s 8

q)x:10*til 10000
q)y:til 100000
q)\ts sum y>/:x
7200 1312358576
q)\ts sum(y>)peach x
5298 1312424112
q)(sum y>/:x)~sum(y>)peach x
1b

Reference: http://code.kx.com/q/ref/control/#peach

Upvotes: 2

Related Questions