Solved

Try/Except structure really slowing down my code execution

Posted on 2014-04-04
3
469 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 25

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now