Solved

Try/Except structure really slowing down my code execution

Posted on 2014-04-04
3
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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