shawn857
asked on
Try/Except structure really slowing down my code execution
Hi Experts...
In a tight While loop (reading records of a data file), I call during each iteration another procedure that does some checks on the data I read in, and performs a StrToFloat operation embedded within a Try/Except block. Running a code profiler showed me that this procedure with the StrToFloat is taking most of the processing time. Googling around I found this page:
http://www.drbob42.com/delphi/perform.htm
(just do a search for StrToFloat on that page). There it says how exception handling in a tight loop really slows things down. My question is, how can I efficiently execute my StrToFloat statement and check whether it returns a valid numeric value or not... without being in the confines of a Try/Except structure?
Thanks!
Shawn
In a tight While loop (reading records of a data file), I call during each iteration another procedure that does some checks on the data I read in, and performs a StrToFloat operation embedded within a Try/Except block. Running a code profiler showed me that this procedure with the StrToFloat is taking most of the processing time. Googling around I found this page:
http://www.drbob42.com/delphi/perform.htm
(just do a search for StrToFloat on that page). There it says how exception handling in a tight loop really slows things down. My question is, how can I efficiently execute my StrToFloat statement and check whether it returns a valid numeric value or not... without being in the confines of a Try/Except structure?
Thanks!
Shawn
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did the trick nicely Sinisa, thank you!
Cheers
Shawn
Cheers
Shawn
in the inner loop you could keep track of where you are
and then when you get the exception start again at the point where the error occured
or prevent the error by happening by checking if the string is a float before converting it
or use TextToFloat which returns a boolean false if the text is not a float