Syntactic Confectionery Delight PerlMonks

### comment on

 Need Help??
So I wanted to see how a sliding buffer compared to a simple push/shift. Maybe I implemented the sliding buffer in a slow way (suggestions?), but for my (likely poor) implementation, the simple push/shift is blazingly faster than the sliding buffer.

Did I blow the buffer implementation, or is native push/shift damn efficient? (probably both)

use strict; use Test::More 'no_plan'; use constant SIZE => 500; use Benchmark qw(:all); ################################################ # ROLL1: sliding buffer ################################################ { my ( \$last, @x ); sub init1 { \$last = SIZE - 1; @x = (undef) x SIZE; } sub roll1 { my (\$val) = @_; \$last = ( \$last + 1 ) % SIZE; \$x[\$last] = \$val; return \@x[&order]; } sub order { my \$first = ( \$last + 1 ) % SIZE; return ( \$first .. SIZE - 1, 0 .. \$last ); } sub dump1 { return join ( '-', @x[&order] ); } } ################################################ # ROLL2: simple push and shift ################################################ { my @x; sub init2 { @x = (undef) x SIZE; } sub roll2 { my (\$val) = @_; push ( @x, \$val ); shift @x; return \@x; } sub dump2 { return join ( '-', @x ); } } ################################################ # ensure both return the same results ################################################ for my \$roll ( 5, 19, 786 ) { &init1; &init2; for ( my \$i = 0 ; \$i < \$roll ; \$i++ ) { my \$val = rand; roll1(\$val); roll2(\$val); } is( dump1, dump2, "same results for \$roll rolls" ); } ################################################ # benchmark them ################################################ timethese(100, { 'roll1' => sub { init1; roll1(\$_) for (1..10000);}, 'roll2' => sub { init2; roll2(\$_) for (1..10000);}, });

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2021-05-08 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Perl 7 will be out ...

Results (96 votes). Check out past polls.

Notices?