Friday, December 24, 2010

Sort of introduction to J.

Firstly, what is J? Yeah, it's a letter in English alphabet. But other than that, J is a programming language. As I suppose, this is a language for lazy mathematicians—so hopelessly lazy that they don't want to type long function names. Thus it's VERY concise language.

Let's consider just one example. Suppose we wanna investigate the sequence of first digits of powers of 2. (1,2,4,8,16,32,64,128,256,...) The question is: what's the frequency of the digit 7? (You can find via search engines several versions of this problem: this, for example.)
Using J, we can find (well, estimate) the answer in one line:
(+%~(#&I.&(7=<.&(10^1|(10^.2)&*&i.)))) 1000000
0.05799
The exact answer is
.
That follows from the equidistribution theorem.

So, what does this mess of symbols mean?

Monday, December 20, 2010

GPS & Ruby: dealing with performance bottlenecks

About a month ago, I posted about how to convert pure GPS data into Ruby class instances. But as it turned out this converting in general takes more time than processing the data.

For tests I've used 2.7MB .nmea file. It contains about 5 thousand GPRMC sentences from which about 3 thousand contain useful information.

The first version of readNMEA function takes about 0.87 seconds (on my Asus EeePC 1001P) to make an array of GPRMC class instances. How can we improve that?

Firstly, access to the elements of an array (by index) is faster than access to the elements of a hash (by key). Thus using old-style regular expressions decreases the time from 0.87 to 0.72 seconds: