Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is there a way to calculate how much cpu time a program uses? 2

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
I am tuning an existing perl code. I want to compare how much cpu time it uses before/after my change.

Is it doable? If so, how?

Thank you for your help.
 
Yea, you can use benchmark to see if one routine is better than another:


Or try using Time::HiRes if the interval times are small:

Code:
use Time::HiRes qw( clock );
my $clock0 = clock();
[your code here]
my $clock1 = clock();
my $clockd = $clock1 - $clock0;

You could even do that with 'time' and $^T if the script runs for longer than a long time. At the very end of your scripts:

Code:
my $time_it_took = time - $^T;

run a good number of tests to get a reasonably accurate result.

- Kevin, perl coder unexceptional!
 
Thank you, Miller and Kevin. I'll test it and update this thread later.

Thanks again.
 
Kevin,

I have neither Benchmark.pm nor Time::HiRes installed yet. I'll talk to my admin later. So I only tested your last solution:

Code:
my $time_it_took = time - $^T;

I tested on a very small program and it should not consume 1 sec of cpu time. But when I tried to print '$time_it_took', I noticed that value is always around 52,000. What is the unit of '$time_it_took'? Micro-second?

Thank you!
 
lcs01 said:
What is the unit of '$time_it_took'? Micro-second?

It should be in seconds. If you are noticing a value of 52k, I suggest that you simply print out the constiuent parts, ie scalar(time) and $^T, and make sure that those are values that you expect.
 
Thank you Miller. 'scalar(time)' is the time in second since 01/01/1970, right? I did print $^T out during my test. And the math is right. But the CPU time can not be that high!!

What's next?
 
Yes, scalar(time) is the number of seconds since epoch, as is $^T. See:

(Search for $^T)

What's next?

Well, I don't know. I doubt that your process is running for over 14 hours, so I surmise that there is something else going on. This is one of those things that you're going to have to debug yourself most likely. I suggested that you bring up the raw values of $^T and scalar(time), so that you could verify that their difference was truely 52k. If it is, then the problem is in the specific values of one of those two variables. So what's next is you need to track down the problem further on your end.

Alternatively, Time::HiRes really is a great module for doing these types of efficiency tests. So hopefully your sysadm responds soon.
 
The difference is TRUELY 52K. I'll talk to my sysadmin. Will update this thread later.

Thank you so much, Miller.
 
the methods I mentioned are not the CPU time, it's probably more like wall clock time. If you do not have the Benchmark or Time::HiRes modules you must be using an old or incomplete installation of perl since they are both core modules.

What is the operating system and version of perl you are uisng?

- Kevin, perl coder unexceptional!
 
Kevin,

We are still using perl 5.6 on redhat (Fedora Core release 3).
:(

I know perl 5.8 has it.
 
5.6 should have Benchmark and I'm pretty sure Time::HiRes too.

- Kevin, perl coder unexceptional!
 
Yes, they are both available, but I do not believe they were part of the original release of 5.6.

perl -MCPAN -e "install Time::HiRes"
perl -MCPAN -e "install Benchmark
 
I have Time::HiRes installed. The results turned out nicely by using Kevin's solution.

Thank you both, Kevin and Miller, very much.
 
You're welcome, thanks for the star.

- Kevin, perl coder unexceptional!
 
You're welcome as well, and thanks for the star.

It's a lot of fun making code more efficient, but make sure you're not wasting time with only minor speed improvements. Because TIMTOWTDI in perl, it can be tempting to research only really minor changes in code.
Coder: "Wow, I sped up this section of code 50%!"
Manager: "That's amazing! How long was it taking before?"
Coder: ".009ms! That's .004ms that we saved! I'm going to go through the entire codebase now and make this change everywhere!
Manager: uuuhhhhhhhh
I personally limit my efficiency testing to only mission critical functions, and of course to obviously time intensive IO operations like SQL statements.

Have fun :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top