Timing Things

Previous pageNext page

"Time the execution of an expression."

Time millisecondsToRun: [1000 factorial].


Time microsecondsToRun: [1000 factorial].


Two separate experiments here, but each one does effectively the same thing. Put the cursor on the first expression and type Ctrl+D. Depending on the speed of your computer you should get a fairly small number (e.g. 3) which is the number of milliseconds it took to run the contents of the block. A block is any piece of Smalltalk code surrounded by square brackets. Don't worry if your answer is 0; it just means you've got a very fast machine!

With the second experiment you are just asking the Time class to perform a more accurate timing and give an answer in microseconds. Again, put the cursor on the line and type Ctrl+D.

If you haven't seen them before, blocks are really cool things. They are not like enclosing stuff in curly braces {} in C++ or Java. In fact, they are objects like anything else in Smalltalk so they can be passed around, stored in variables etc.  The code inside the square brackets [] isn't actually executed until the block object is asked to evaluate itself by sending it a #value message. At some point that #microsecondsToRun: message above will have sent #value to the [1000 factorial] block to cause it to be executed.

You'll see that blocks are used all over the place in Smalltalk; they are used for iterating over collections and they are even used for conditional (IF) expressions but always remember they are objects and not just a piece of syntax handled by the compiler.

Remember, to get back to these experiments just choose Help/First Splash in any Dolphin tool window.