Loops in Smalltalk

Kenneth Johnsen kennethjohnsen at mail.dk
Sun Jun 27 16:49:23 UTC 2004

I read an article about the new version of Dataflex, where it said that 
the time it took to do 5000 get/set operations had been reduced from 58 
to 3 seconds.

I read it like: set and get an instance variable on an object 5000 
times, but I presume that something more advanced is going on 
(otherwise, it's not that impressive).

But this got me wondering: how long does it take in Smalltalk?

So, I made a small class, with one instance variable: instancevar, and 
three methods: instancevar, instancevar: and runTest. The two former 
just gets/sets instancevar, and runTest does:

    | anInt |
    ^ Time
        millisecondsToRun: [1
                to: 5000
                do: [:z |
                    self instancevar: z.
                    anInt := self instancevar.
                    Transcript show: anInt printString;

Now, I didn't have the "Transcript show" in it the first time I ran it.

Squeak printed: 1
Dolphin printed: 1

Question 1: Very nice. But am I missing something, where the compiler 
figures out that my instancevar is not used for anything else, and 
ignores it?

Next, I put the "Transcript show" in (I know, I know, that's not exactly 
testing the loop itself).

Squeak printed: 125585
Dolphin printed: 20711

Presumably Dolphin is faster because it is targeted at Windows.

Question 2: After "5000" was printed, both systems took a couple of 
seconds to come alive again (i.e. in Squeak, everything froze, in 
Dolphin, I got an hourglass). What were they doing? Was that the garbage 
collector at work, or?


Kenneth Johnsen.

More information about the Squeak-dev mailing list