in reply to Re: Zero Padding
in thread Zero Padding
UpdateOops, fixed the bug, tye's right that it doesn't change the result much, but I didn't really want to return the correct value in concat() or make spr_f() extra fast, just wanted to compare the process to create the desired result.#!/usr/local/bin/perl use Benchmark; my $var=13; my $length=6; timethese(100000, { CONCAT=>\&concat, SPRINTF=>\&spr_f, }); sub concat { my $a = $var; while (length($a)<$length) {$a="0$a";} } sub spr_f { my $a = $var; sprintf("%0$length", $a); } Results: Benchmark: timing 100000 iterations of CONCAT, SPRINTF... CONCAT: 2 wallclock secs ( 1.53 usr + 0.00 sys = 1.53 CPU) @ 65 +359.48/s (n=100000) SPRINTF: 0 wallclock secs ( 0.60 usr + 0.00 sys = 0.60 CPU) @ 16 +6666.67/s (n=100000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re: Zero Padding
by tye (Sage) on Feb 03, 2001 at 04:08 UTC | |
by dkubb (Deacon) on Feb 03, 2001 at 08:33 UTC | |
by tye (Sage) on Feb 03, 2001 at 09:42 UTC | |
|
Re (tilly) 3: Zero Padding
by tilly (Archbishop) on Feb 03, 2001 at 05:11 UTC | |
|
Re: Re: Re: Zero Padding
by Anonymous Monk on Feb 03, 2001 at 03:49 UTC | |
by yakko (Friar) on Feb 03, 2001 at 10:27 UTC |