Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Try/Except structure really slowing down my code execution

Posted on 2014-04-04
3
Medium Priority
?
501 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 2000 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 38

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

Technology Partners: 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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

715 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