[Q] Newbie - Multiple Return Values

Richard A. O'Keefe ok at cs.otago.ac.nz
Mon May 5 03:35:36 UTC 2003


John M McIntosh <johnmci at smalltalkconsulting.com> wrote:
	Can't say I've ever seen the block example in code that I've
	look at so I thought it was neat.  Mind I wonder about the
	expense of doing the block evaluation etc etc from a performance
	viewpoint.
	
Well, in some Scheme systems that I've benchmarked, the block method
is easily the fastest way to return multiple results.  When it comes
to Squeak, "when in doubt, MEASURE!"

1.  Open a browser and make a new class, MVTime.
    Give it instance variables x, y.
2.  Give it an instance method 'x: this y: that x := this. y := that.'
3.  Give it an instance method 'xy ^{x. y}'
4.  Give it an instance method 'report: ^aBlock aBlock value: x value: y'
5.  Give it an instance method 'point: ^x at y'

6.  Open a workspace.
7.  Type in the setup code
    mvt := MVTime new
    a := x := y := this := that := nil.
    and DoIt.
8.  Type in the timing code
    [100000 timesRepeat: [
     mvt x: 1 y: 2. a := mvt xy. x := a at: 1. y := a at: 2
    ]] timeToRun.
    and PrintIt.
9.  Type in the timing code
    [100000 timesRepeat: [
     mvt x: 1 y: 2. mvt report: [:this :that | x := this. y := that]]
    ]] timeToRun
    and PrintIt
10. Type in the timing code
    [100000 timesRepeat: [
     mvt x: 1 y: 2. a := mvt point. x := a x. y := a y
    ]] timeToRun

For the Array-returning code, I get 1125.
For the Block-passing code,   I get  867.
For the Point-returning code, I get  540.

These numbers aren't exactly surprising.
Put it this way, if you have reason to suspect that the object returned
here is likely to acquire new responsibilities and/or new uses, return
a purpose-built object.  If you don't have such reason (division is not
likely to return more than a quotient and a remainder) using a block
requires the least coding and is at least better than using an Array.



More information about the Squeak-dev mailing list