Brace Bracket assignment for two arguments vs >>become: (Was:
Squeak programming question)
Andrew C. Greenberg
werdna at gate.net
Tue Mar 23 01:40:11 UTC 1999
> First test: Time millisecondsToRun:[ a become: b ] ==> 23
> Second test: Time millisecondsToRun:[ {a.b} := {b.a} ] ==> 0
>
> What the heck is going on with this curly brackets? How can this be that
> much faster? Did I use a bad test bed?
Yes. assignment is way too fast to register on this scale. become:
is another story -- and it probably doesn't do what you seem to want
it to do. Let's take the second point first.
Here is the Squeak comment for become:
Primitive. Swap the object pointers of the receiver and the argument.
All variables in the entire system that used to point to the
receiver now point to the argument, and vice-versa.
Fails if either object is a SmallInteger
become: is a very expensive operation in Squeak. In other
Smalltalks, where an object table is maintained, become: can be
implemented with a simple swap of pointers in the object table. In
Smalltalk, the VM has to find EVERY REFERENCE to a and b in the
image and replace it with a pointer to b and a, respectively, an
extensive computation. And you really, really, don't want to do
becomes with special operators, like nil, which may account for your
crashes.
On the first point, modern machines run Smalltalk fast, really fast.
Accordingly, don't be surprised that when counting integer
milliseconds, a coupla' stack pushes and pulls of a pointer doesn't
even register on the scale. To measure performance meaningfully, you
need to run the test inside a loop, something like:
Time millisecondsToRun: [
1000 timesRepeat: [
{a.b} := {b.a}]]
This will register something in the small decades on your machine.
More information about the Squeak-dev
mailing list
|