Integer micro-benchmarks

Steve Wart swart at deadspam.com
Wed Apr 25 22:31:29 EDT 2001


Here is some more silliness.

If you take the #triangle method and turn it into a block closure, as below,
in VW it takes almost 10 seconds (on my 333 PII)

"c.l.s. benchmark"
|sum time triangle result |
sum := 0.
triangle := [ :int|
 result := 0.
 1 to: int do: [ :i | result := result + i].
 result ].

time := Time millisecondsToRun: [
10000000 timesRepeat: [
sum := sum + (triangle value: 10)]].
^Array with: sum with: time.

If you modify the above code so that the "result" instance variable is a
block temp, i.e.

triangle := [ :int| | result |
 result := 0.
 1 to: int do: [ :i | result := result + i].
 result ].

It executes in under 7 seconds (same as if #triangle were a method on
Integer).

Something to do with the reification of the block temp I suppose [I only
know about this from reading a post by Allen Wirfs-Brock on the Squeak list
this am] :) Smalltalk MT does not allow block temps.

Does python have closures? That would be interesting to see.

Cheers,
Steve






More information about the Python-list mailing list