
mhx
Enthusiast
/ Moderator
Mar 6, 2002, 8:26 AM
Post #16 of 19
(1294 views)
|
|
Re: [RedRum] **COUNTING** unique lines input
[In reply to]
|
Can't Post
|
|
Not sure but the perl peeps say print is much more efficient for printing that printf. That's correct. So in applications where speed is the primary goal, forget Perl's printf. [perl] #!/usr/bin/perl -w use strict; use Benchmark; my $bigstring = <<'EOS'; meis novis da XLIV tum X. kcahere sic face cis rere sic da loco huic his decapitamentum. meo novo. dum hoc fac sic praecidementum huic da meo nexto. cum novum tum nextum serementum novo da cis novum redde EOS my $number = 0xDEADBEEF; my $float = 3.1415926535; timethese( 100000, { a => sub { print STDERR q(Here's a string: ) . $bigstring . q(A number: ) . $number . "\n" . q(And a float: ) . $float . "\n"; }, b => sub { print STDERR qq(Here's a string: ${bigstring}A number: $number\nAnd a float: $float\n); }, c => sub { printf STDERR q(Here's a string: %sA number: %u%cAnd a float: %.10f%c), $bigstring, $number, 10, $float, 10; }, d => sub { printf STDERR qq(Here's a string: %sA number: %u\nAnd a float: %.10f\n), $bigstring, $number, $float; }, }); [/perl]
Benchmark: timing 100000 iterations of a, b, c, d... a: 2 wallclock secs ( 1.87 usr + 0.47 sys = 2.34 CPU) @ 42735.04/s (n=100000) b: 1 wallclock secs ( 1.71 usr + 0.48 sys = 2.19 CPU) @ 45662.10/s (n=100000) c: 8 wallclock secs ( 6.16 usr + 0.63 sys = 6.79 CPU) @ 14727.54/s (n=100000) d: 7 wallclock secs ( 6.04 usr + 0.63 sys = 6.67 CPU) @ 14992.50/s (n=100000) That makes print about 3-4 times as fast as printf. -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|