in reply to Which is faster ?

A better thing to wonder, in my opinion, would be, "how can I find out which parts of my program take the most time and would be most worth optimizing?" For this, there is Devel::SmallProf, Devel::NYTProf, and more documented in Profiling your code.

Another better thing to wonder would be, "how can I write this in such a way that my coworkers and I spend the least time maintaining it?"

Replies are listed 'Best First'.
Re^2: Which is faster ?
by thenetfreaker (Friar) on Jun 04, 2008 at 17:23 UTC
    Thanks for the helpful docs.
    But I prefer measuring my program's time by:
    my $t1 = time(); ... ...somecode... ... my $t2 = time(); print "It took ".(int(($t2-$t1)/3600)).':'.(int((($t2-$t1) % 3600)/60) +).'.'.(int(($t2-$t1) % 60))."\n";

    And outputing from every func() all its main variables.

      One problem with that is it doesn't really tell you how much of that time is spent in your program and how much is spent by your program waiting (for a database, or the network, or a disk drive, or another program getting a time slice, etc.). There's not much point in trying to speed up a piece of code that does nothing but wait for something out of your control.

      If you still like that style of timing anyway, you could get better results with Time::HiRes.

      Apples and oranges. Profiling is not about timing; profiling tells you 'where' your code spends it's time. Where your code 'is' useful is timing long runs of linear code whereas the Benchmark module is useful for snippets of code and getting at the internals of subroutines without measuring the subroutine call itself. As to just storing time(), actually I've done that myself on occasion but usually I use the Time::HiRes module.


      s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}