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

benchmark comment

by perlmonkey (Hermit)
on Jul 27, 2000 at 08:27 UTC ( #24630=note: print w/replies, xml ) Need Help??


in reply to RE: RE: Set String Length (benchmark)
in thread A Set String Length

Be careful with benchmark, your test is not a real test because your $str is lexical ... it is not defined in the 'pack' and 'sprintf' subroutines. You have to declare the lexical variable inside the routines. There can be significant differences between your test and a real test (although the pack and sprintf are still similar in performance):
my $str = 'hello'; timethese( 1000000, { 'pack' => q{$str = pack("A10",$str); }, 'sprintf' => q{$str = sprintf("%-10s",$str); }, });
Results:
Benchmark: timing 1000000 iterations of pack, sprintf... pack: 3 wallclock secs ( 3.84 usr + 0.02 sys = 3.86 CPU) sprintf: 4 wallclock secs ( 4.24 usr + 0.00 sys = 4.24 CPU)
The correct way:
timethese( 1000000, { 'pack' => q{my $str = 'hello'; $str = pack("A10",$str); +}, 'sprintf' => q{my $str = 'hello'; $str = sprintf("%-10s",$str); +}, });
Results:
Benchmark: timing 1000000 iterations of pack, sprintf... pack: 6 wallclock secs ( 5.82 usr + 0.03 sys = 5.85 CPU) sprintf: 7 wallclock secs ( 6.80 usr + 0.01 sys = 6.81 CPU)

Replies are listed 'Best First'.
RE: benchmark comment
by steveAZ98 (Monk) on Jul 27, 2000 at 08:36 UTC
    Ah.. thanks for the tip, didn't even realize I was doing that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://24630]
help
Chatterbox?
[marto]: hey Corion, good weekend?
[Corion]: marto: Yeah, even though I didn't write any code :)
[Corion]: But at least I have a plan of action to move the site to https, played some (free!) VR games with friends and watched the plans for the next German Perl workshop progress ;)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2017-08-21 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (319 votes). Check out past polls.

    Notices?