Solved

Try/Except structure really slowing down my code execution

Posted on 2014-04-04
3
479 Views
Last Modified: 2014-04-05
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
0
Comment
Question by:shawn857
3 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39977723
you can check this way:
var
  f: double;
...
  if TryStrToFloat(s, f) then
  begin
    //f is float ....

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39979962
bring the exception to a higher level
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
0
 

Author Closing Comment

by:shawn857
ID: 39980816
That did the trick nicely Sinisa, thank you!

Cheers
   Shawn
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question