Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

old code revisited

by neophyte (Curate)
on Sep 08, 2001 at 17:23 UTC ( #111156=perlmeditation: print w/ replies, xml ) Need Help??

In the section Q&A under Is there a shorter way to create a german time string? I posted some code that's still running in some of my older scripts. Yesterday I got back to the snippet in a printed version and wondered.
I knew I did a benchmark with using localtime and Date::Manip and performance didn't differ significantly for me. Now I still wondered if I could make that code more efficient.
So I tried the following benchmark:
#! /usr/bin/perl -w use strict; use Benchmark qw(timethese); sub zeit_mit_func { # old function calling localtime several times (slightly modified) my $wtag = ("So","Mo","Di","Mi","Do","Fr","Sa")[(localtime)[6]]; my $mtag = sprintf("%02d", (localtime)[3]); my $monat = sprintf("%02d", (localtime)[4]+1); my $jahr = (localtime)[5] + 1900; my $stunde = sprintf("%02d", (localtime)[2]); my $minute = sprintf("%02d", (localtime)[1]); my $zeit = "$wtag $mtag\.$monat\.$jahr - $stunde\:$minute"; return $zeit; } sub zeit_mit_array { # new function calling localtime only once assigning it to an array my @zeit = localtime; my $wtag = ("So","Mo","Di","Mi","Do","Fr","Sa")[$zeit[6]]; my $mtag = sprintf("%02d", $zeit[3]); my $monat = sprintf("%02d", $zeit[4]+1); my $jahr = $zeit[5] + 1900; my $stunde = sprintf("%02d", $zeit[2]); my $minute = sprintf("%02d", $zeit[1]); my $zeit = "$wtag $mtag\.$monat\.$jahr - $stunde\:$minute"; return $zeit; } timethese (1E5, { 'func' => \&zeit_mit_func, 'array' => \&zeit_mit_array, });
The result was (as I had expected)
Benchmark: timing 100000 iterations of array, func... array: 11 wallclock secs (10.82 usr + 0.00 sys = 10.82 CPU) @ 92 +42.14/s (n=100000) func: 33 wallclock secs (32.68 usr + 0.00 sys = 32.68 CPU) @ 30 +59.98/s (n=100000)
on a WinME with ActivePerl Build 628. I also tested it on a FreeBSD 4.0, Perl 5.005 on an old notebook, where the difference was even more significant.

So this is to show - never trust old code. Don't get into the habit of copy and paste.
Make it a habit to review old code, It'll do your code and your coding expertise good.
It already has helped me gain some insights - and I'm sure it'll continue doing so.

neophyte Niederrhein.pm

Comment on old code revisited
Select or Download Code
Re: old code revisited
by myocom (Deacon) on Sep 08, 2001 at 19:17 UTC

    Also, it's best to call localtime only once and do your manipulations on that (as you did in the array version). If your function version had started at 23:59:59 and took any longer to run than a second, your results would have been wonky. Looks like you got lucky all this time. :-)

    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: old code revisited
by Albannach (Prior) on Sep 08, 2001 at 20:03 UTC
    Make it a habit to review old code, It'll do your code and your coding expertise good.

    Indeed! This doesn't just apply to copy and paste either, as I often find things that can be done better or simply coded more clearly when I pull out some little program I wrote 6 months ago. I take the fact that I'm using it a second time as a sign that a little more investment of effort could well pay off in the future. Recently I have found myself replacing silly little loops with map or grep (as appropriate) and finding the clarity of the code improves significantly.

    On your specific example, you could go a step farther and eliminate the multiple sprintf calls. On my box (Win95, ActiveState 628) this appears to be 20% faster than the array improvement alone:

    sub zeit_mit_Albannach { # new function calling sprintf only once my @zeit = localtime; my $wtag = ("So","Mo","Di","Mi","Do","Fr","Sa")[$zeit[6]]; sprintf("$wtag %02d.%02d.%4d - %02d:%02d", $zeit[3], $zeit[4]+1, $zeit[5] + 1900, $zeit[2], $zeit[1]); }

    --
    I'd like to be able to assign to an luser

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://111156]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2014-08-22 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls