A colleague of mine told me how he'd measured the time to calculate the square root of 10 million numbers as part of the evaluation of a product. One of the suprising things is just how fast this is on a modern average specification PC. Have a guess how long this would take in Java doing a simple loop implementation. Answer at the end - it might suprise you.
It seemed to me that this "problem" would be just the sort of thing you could do in the language J very simply; here's some code that works (whether this is how a J master would do it is quite another question):
%: ?10000000#10000000
Yes - that's all!
to explain:
10000000#10000000
gives you a list of 10000000 numbers, each of value 10000000
?10000000#10000000
gives you a list of 10000000 random numbers, each bewteen 0-10000000 (it applies the ? function to every element in the list, ? being a function that gives you a random number from 0 - it's parameter)
%: is the square root function, so %: ?10000000#10000000 gives you a list which is the square root of each number in the list.
So - to answer the speed question, on a desktop machine, the Java version took about half a second. (The speed of the J version will have to wait until I find out how to time things in J, probably really easy but I just haven't bothered to look yet; I was more interested in the neatness of the code rather than the performance).
Posted by ivan at December 24, 2004 7:33 PM