in reply to how to speed up program?

The foreach loop is very slow (takes 2 days to run) because there are 31,197 alarms each analyzing 155 Quench Times. I wrote sub GetQuenchInfo to create a hash to try to speed up...

You made two mistakes here: First, you started optimizing within a loop before you considered what you could move out of the loop. Second, you began pursuing a particular optimization (creating a hash) before you'd gathered data on where your script was spending its time.

Making a database connection and running the queries can be moved out of the loop, since nothing they do depends on $supply, which is the only thing varying in that loop. (Is this intentional, or a bug?) But for a moment, let's pretent they can't be moved out of the loop.

Since you know the performance problem is in the loop, a reasonable approach is to pick the loop apart, looking for where the script might be spending time. Then add some simple timing code to gather data. For example:

my $aboutToConnect = time(); # start timing my $dbEvents = new Sybase::CTlib 'harmless','harmless','OPSYB1','fil +leventsT'; print "Sybase::CTlib took ", time() - $aboutToConnect, " seconds to connect\n";
You might then have discovered that making a new database connection is relatively expensive.

Ditto for timing the queries.

If you discover that 99% of your time is going to database work, then trying to improve performance by using a hash is pointless, unless reducing that 1% to 0.5% is going to pay off.

Replies are listed 'Best First'.
Re: Re: how to speed up program?
by Anonymous Monk on Aug 16, 2002 at 22:33 UTC
    Um, time has only 1 second resolution. For him to discover the time wasted, he needs to install Time::HiRes and then call gettimeofday.