Solved

Try/Except structure really slowing down my code execution

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

19 Experts available now in Live!

Get 1:1 Help Now