Benchmarking functions in parallel with threads in Delphi
Benchmarking functions in parallel with threads in Delphi
First, we need to start with a default VCL application in Delphi. Because I do not know the ways that Lazarus handles threads I make no promises that it carries over. So many people are concerned about threads, their dangers, and potential problems with exceptions that I figured I would show you how to benchmark faster on multicore computers. Safely so.
First, get a decent UI consisting of a pair of labels, one indicating which function is being tested and the other for the average execution time, a default progress bar, and two buttons for testing 10,000 and 100,000 times. Why just 10,000 and 100,000? Well, doing 1,000,000 can take a while to accomplish. Also, make sure your progressbar has "step" set to 1, not 10.
Now, above your form object insert this blank template code:
Code:
TBenchmarkThread = class(TThread)
private
constructor Create(CreateSuspended: boolean = false);
protected
fTimes: Cardinal; // The number of times we execute the same function.
fProg: TProgressBar; // Our Progressbar (multithreaded progressbar calls!).
fS: String;
procedure Execute; override;
end;First, why these pieces? Well, the function I will be testing is StrToInt and I'll be testing 10000 and -10000 conversions simultaneously; that means half the tests are negative, half are positive. With the internal progressbar reference we can easily indicate the process's progress during these tests.
Now, double-click the first benchmark button, the one for the 10000, and enter this code into the function that was created.
Code:
Benchmark(TButton(Sender).Tag);This lets us assign this to the other button too, which you should do, and set the tags of each button to the appropriate number. Once this is done define a private benchmark function in your form. Some of these functions and properties aren't around yet, but don't panic.
Comments
09 Jan 2009, 4:23 

