The problem is not with vec, it is the way I start at the start of the string and count off non set bits till I reach $rand. The following timings are on a dev machine being used by a few so there is plenty of noise. The time to read the last bits of a long string using vec looks constant even with strings spanning several orders of magnitude.

#!/usr/local/bin/perl -w use strict; use Benchmark qw(timethese); timethese(5000000, { '1K' => vectest(1000), '10K' => vectest(10000), '100K' => vectest(100000), '1M' => vectest(1000000), '10M' => vectest(10000000), }); sub vectest { my $n=shift; my $s=""; vec $s,$n,1; my $look=vec $s,$n-10,1 } __END__ Benchmark: timing 5000000 iterations of 100K, 10K, 10M, 1K, 1M... 100K: 0 wallclock secs ( 0.59 usr + 0.00 sys = 0.59 CPU) 10K: 1 wallclock secs ( 0.42 usr + 0.00 sys = 0.42 CPU) 10M: 1 wallclock secs ( 0.49 usr + 0.00 sys = 0.49 CPU) 1K: 1 wallclock secs ( 0.89 usr + 0.00 sys = 0.89 CPU) 1M: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51 CPU)

I have a serious optimisation up my sleave but and rather too busy to code it now :( Hope to have a shot when I get home this evening

Cheers,
R.


In reply to Re^3: Generating 0 .. N Randomly and Efficiently by Random_Walk
in thread Generating 0 .. N Randomly and Efficiently by Limbic~Region

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.