Timing Anonymous Methods

Developers frequently add timing code to existing routines to compare their relative speed. I did the same a few times in examples in Part I of the book, to figure out the speed of Unicode strings. Supposing you have two code fragments and you want to compare their speed by executing them a few million times, you could write the following (which is taken from the String-Convert example of Chapter 2 and discussed in the section "Converting Strings):

procedure TFormAnonTiming.btnClassicClick(

Sender: TObject); var stri: string; str2: AnsiString; I: Integer; ti: TDateTime; begi n stri := 'Marco Cantò'; ti := Now;

for I := 1 to MaxLoop2 do stri := AnsiUpperCase (stri); ti := now - ti;

Memoi.Lines.Add ('AnsiUpperCase (string): ' + FormatDateTime( 'nn:ss.zzz', ti));

for I := i to MaxLoop2 do str2 := AnsiUpperCase (str2); ti := now - ti;

Memoi.Lines.Add ('AnsiUpperCase (AnsiString): ' + FormatDateTime( 'nn:ss.zzz', ti));

Rather than repeating the timing code over and over, you can write a function with the timing code that would invoke the code snippet through a parameterless anonymous method:

function TimeCode (nLoops: Integer; proc: TProc): string; var ti: TDateTime; I: Integer; begi n ti := Now;

procedure TFormAnonTiming.btnAnonClick(Sender: TObject); var stri: string; str2: AnsiString;

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment