March 09, 2005

Efficient operations

Ok, I was thinking about picking points in a sphere again. I put together a Perl program that timed picking a million points, then took the average time over 100 trials. Here are the results:
Exact method:   0.37092802 seconds
Discard method: 0.63045970 seconds
In other words, taking the time to figure out the exact method comes close to halving the time it takes to pick a point at random from inside a sphere, which means the extra calculations required for the exact method must not be very expensive.

While I was at it, I did some very rough timings of the cost of various basic operations.
+    1.0
- 1.0
* 1.0
/ 1.0
% 1.0
rand 0.85
cos 1.2
sin 1.2
log 1.2
sqrt 1.0
** 1.5
The test can't be made much more accurate due to my using an interpreted language on an OS that's constantly running other processes in the backround, so don't take the numbers too seriously. All this shows is that rand is cheaper than +, -, *, /, %, and sqrt, which are cheaper than cos, sin, and log, which are cheaper than **, in Perl. Weird, huh?


At March 09, 2005, Blogger Fraxas said...

Actually, it doesn't show that for Perl. It shows that for the particular perl interpreter you're using, on the particular hardware you're using.

In scotland, there exists at least one sheep, at least one side of which is black!

At March 09, 2005, Blogger JeremyHussell said...

For those of you who didn't go through high school and university with the two of us:

One day a layman, a scientist, and a mathematician are sitting together on a train travelling through Scotland. Out the window, the layman spots a black sheep standing alone in the middle of a field. Pointing, he says "Look! In Scotland, sheep are black!" Correcting him, the scientist says "No no, in Scotland some sheep are black." The mathematician just sighs and shakes his head, and says "In Scotland there exists at least one field, containing at least one sheep, at least one side of which is black."


Post a Comment

<< Home