i’m interested in k, so i studied?q’s implementation of cross.
specializing cross a little:
? cross:{n:#m:&(#x)##y;x[m],'n#y}
? cross[r;r]
(“eaxeax”;“eaxecx”;“eaxedx”;“ecxeax”;“ecxecx”;“ecxedx”;“edxeax”;“edxecx”;"edx..
now, i think cross is a bit more than an idiom (or is it?) ?so do k programmers write their own little library of functions like cross? ?or is such a library simply q?
ta, jack.
It’s just in q, but I’m sure you can find legacy k code all over if you’re looking for it (though Kx recommends sticking to q for new development). You can call functions from k if you access it via it’s fqn i.e. .q.cross and you’ll see this done in k code periodically.?
cheers tim,
i just timed cross and ,:/:?
q)\
? a:!2000
? \t a,/::a
2419
? \t .q.cross[a;a]
2459
but to produce the same result as .q.cross:
? \t ,/a,/::a
2448
hmmm…
> 1) .q.cross is different than what you’re typing in (see the code).
yeah, i was comparing ,/:: and .q.cross
> 2) taskset your process and try again.
> 3) If you run them multiple times are their average run times close (mall=
oc)
$ taskset 01 ~/q/l32/q
q)\
\t do[3;.q.cross[a;a]]
7019
\t do[3;a,/::a]
5780
anything i’m missing? .q.cross looks faster… but maybe there’s a
price for more general code? i don’t know.
charset=us-ascii
Message-Id: <118E27FB-8DB7-4F1E-B723-FFC8C672071C@gmail.com>
Cc: “personal-kdbplus@googlegroups.com”
X-Mailer: iPhone Mail (9A405)
From: Timothy Rieder
Subject: Re: [personal kdb+] learning each /: : [off topic]
Date: Sat, 14 Jan 2012 02:42:01 -0500
To: “personal-kdbplus@googlegroups.com”
I’d check in another terminal via top that the last CPU q runs on is always t=
he same (I think col j)
I’ll read the code for cross this weekend at some pt. It’s quite late for me=
:-)
charset=us-ascii
X-Mailer: iPhone Mail (9A405)
In-Reply-To:
Message-Id: <56014890-97FC-41E6-AF89-08215E59C48C@gmail.com>
Date: Sat, 14 Jan 2012 02:45:57 -0500
To: “personal-kdbplus@googlegroups.com”
Mime-Version: 1.0 (1.0)
One last thing: the way you executed taskset it only locks your q to those c=
ores. You need to taskset every other process on the box so they don’t run o=
n the same cores as q.
you might have thought my numbers were odd - i got them running on a Pentiu=
m M.
running on a core2duo (64bit):
q)\
a:!4000
\t do[3;a,/::a]
5519
\t do[3;.q.cross[a;a]]
6317
\t do[3;,/a,/::a]
7626
so it’s more expensive to use ,/a,/::a instead of .q.cross
but if you don’t need to raze a,/::a, then this appears faster.
annoyingly, i can’t define:
X:{x,/::y}
a:!3
and then use X:
a X a
'type
so i have to write something like X[a]a, which is just not as nice as
being able to use infix cross: a cross a .
X:{x,/::y}
a:!3
a X a
'type
If you put it in the .q namespace you can execute infix. It’s not recommend to put things in there though.
q).q.X:{x,/::y}
q)a X a
0 0 0 1 0 2
1 0 1 1 1 2
2 0 2 1 2 2