I had quite an interesting result when I looked further into the performance of this type of function..
I ran the test 10 times, starting with a 0% failiure rate for the inner procedure and increasing each time by 10%.
Here's my code: Note label1 is a valid control on the form.
Code:
var
count : integer;
loop, bigloop : integer;
start : cardinal;
fred : TLabel;
begin
for bigloop := 0 to 10 do
begin
count:=0;
fred:=nil;
start:=getTickCount;
for loop:=1 to 1000000 do
begin
inc(count);
if random*10 > bigloop then fred := label1
else fred := nil;
if (fred<>nil) then
fred.caption:='Hello';
end;
memo1.lines.add('No Try : '+intToStr(getTickCount-start));
count:=0;
start:=getTickCount;
for loop:=1 to 1000000 do
begin
try
inc(count);
if random*10 > bigloop then fred := label1
else fred := nil;
fred.caption:='Hello';
except
end;
end;
memo1.lines.add('Try : '+intToStr(getTickCount-start));
memo1.lines.add(' ');
end;
end;
And it turns out that if your function works fine almost all the time (ie. a best case), a try except clause is actually *slightly* faster! I guess this is because the Non-Try code has one extra If Then statement. But from about 10-15% failiure rate, the overhead of the try except clause can be seen, and it is pretty big...
P.S. Dont quote me here. Does it look believeable to you? Have I got my 'math' right?
Bookmarks