Is there any way that I can get PERL to give me the elapsed time that it took to calculate something? I'm trying to make my algorithm for finding prime numbers more efficient, and obviously I can't tell the differences in the time it took to generate the list, so I need a way to say "List generated in X seconds".
Any help would be much appreciated.

Recommended Answers

All 4 Replies

You can try the Benchmark or Time::HiRes modules, they come with perl. Read the documentation and try a few things, if you get stuck post back.

Thanks very much. The only question I have is as to why it says that it took a negative number of wallclock seconds. The longer it takes, the lower the number, essentially. Do you know why this is?

The Benchmark documentation says:

CAVEATS

Comparing eval'd strings with code references will give you inaccurate results: a code reference will show a slightly slower execution time than the equivalent eval'd string.

The real time timing is done using time(2) and the granularity is therefore only one second.

Short tests may produce negative figures because perl can appear to take longer to execute the empty loop than a short test; try:

timethis(100,'1');

The system time of the null loop might be slightly more than the system time of the loop with the actual code and therefore the difference might end up being < 0.

Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.