in reply to Re: Re: Unexpected: unpack slower than several substr's. Why?
in thread Unexpected: unpack slower than several substr's. Why?

Actually, I had previously tried it using assignment rather than push and the difference was so negligable as to be within the bounds of varience from one run to the next.

C:\test>226666 Benchmark: running substr_it, unpack_it , each for at least 3 CPU seconds ... substr_it: 4 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 23 +75.99/s (n=7520) unpack_it: 4 wallclock secs ( 3.38 usr + 0.00 sys = 3.38 CPU) @ 43 +71.56/s (n=14754) Rate substr_it unpack_it substr_it 2376/s -- -46% unpack_it 4372/s 84% -- Results tally C:\test>

I did verify the original as best I could, but as he didn't supply the whole benchmark I had to make a few guesses.

I also attempted to improve both as far as I could as you will see from the update to my first post based upon your suggestion :^)

Older benchmark code for the above results

#! perl -slw use strict; use vars qw[$line @s @s2 @u1 @u2]; use Benchmark qw[cmpthese]; $line = (join'','a' .. 'y') x 40; sub substr_it{ my @bits; $bits[0] = substr $_[0], 413, 30; $bits[1] = substr $_[0], 373, 30; $bits[2] = substr $_[0], 343, 30; $bits[3] = substr $_[0], 245, 15; $bits[4] = substr $_[0], 679, 30; $bits[5] = substr $_[0], 10, 10; $bits[6] = substr $_[0], 900, 100; $bits[7] = substr $_[0], 500, 2; $bits[8] = substr $_[0], 313, 30; $bits[9] = substr $_[0], 844, 44; $bits[10] = substr $_[0], 111, 1; $bits[11] = substr $_[0], 100, 100; $bits[12] = substr $_[0], 454, 30; $bits[13] = substr $_[0], 240, 3; $bits[14] = substr $_[0], 236, 4; @bits; } sub unpack_it{ my $fmt = '@413A30 @373A30 @343A30 @245A15 @679A30 @10A10 @900A100 +' . '@500A2 @313A30 @844A44 @111A1 @100A100 @454A30 @240A3 @ +236A4'; unpack( $fmt, $_[0]); } cmpthese( -3, { substr_it => '@s = substr_it $line;', unpack_it => '@u1 = unpack_it $line;', }); print 'Results tally'; __END__

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re: Re: Re: Re: Unexpected: unpack slower than several substr's. Why?
by steves (Curate) on Jan 14, 2003 at 03:16 UTC

    I'd have to take back my educated guess and go with BrowserUk. I tried this on Solaris and Linux using several different variations and the substr version always comes out over 40% slower. And I did not see the push being twice as slow as the explicit array assignments on either system.

    Besides platform, the data being operated on would be the only other factor I can think of that might make the original so much different from what others have tried.