To write a bench method, you subclass the standard bench class EsbBench. The new subclass is stored in the library and is visible to all Smalltalk browsers. Next, you write a method in the new subclass. Bench methods must start with an uppercase letter or they will not be visible from the Stats user interface.Because the Stats tool has fewer editing features than a Smalltalk browser, you might write the methods in a Smalltalk browserA bench method is a Smalltalk method that contains selected code from an application program. Areas of interest are timed using instances of high-precision software timers, which are available as inherited instance variables of EsbBench.EsbBench provides inherited instance variables, which are automatically initialized and ready to use in all subclasses.EsbBench reimplements the class method new to send initialize to the new instance after it has been created. Any subclass of EsbBench must send super new when reimplementing new, and super initialize when reimplementing initialize. Failure to do so causes the inherited instance variables of EsbBench to be uninitialized, and the Stats tool will not execute the method.See Timing sections of code for an example of how to use the instance variables. The instance variables are as follows:An integer used in conjunction with timesRepeat: to control the number of times an operation runs. The default is 10.A high-precision software timer used to remove a fixed overhead from the operation. As with benchTimer, you can start and stop a differenceTimer.Using benchTimer, you surround critical portions of the code. The above example includes the time to open a window but ignores the time to close it. The iterations loop surrounds the start and stop messages to benchTimer, allowing benchTimer to collect statistics on every iteration.In the case of operations that take less than one millisecond, have the start and stop messages surround the iterations loop. The loop must be included in the operation because the time to execute the operation is so short that it cannot be measured from Smalltalk. To remove the cost of the loop and any other fixed cost of the operation, use the differenceTimer.Writing a bench method has the following advantages over repeatedly executing the expression Time millisecondsToRun: ...] in a workspace and viewing the results:
• Benchmark execution is monitored and controlled by the Stats tool user interface.The Stats is unobtrusive; raw stack data is gathered while the benchmark code executes but is not analyzed until execution completes.While bench code is often built and executed through the Stats user interface, it can also be constructed from other browsers and executed without the user interface. Over time, while optimizing the code, the programmer builds up a suite of benchmarks, which can be run periodically to ensure that code does not become slower.
|