Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
The following is a slight modification of your program. It (ab)uses the fact that Perl only checks whether to split buckets when you use a new bucket. It does not make any assumptions about Perl's hashing algorithm. It is therefore able to generate the special keys much more quickly than yours does.
my $HOW_MANY = shift || 100_000; my ($s, $e1, $e2); $s = time; for ($i=0; $i<$HOW_MANY; $i++) { $q{$i}=1; } $e1 = time - $s; print qq{ Normally, it takes me about $e1 second(s) to put $HOW_MANY items into +a hash. Now I'm going to construct $HOW_MANY special keys and see how long it takes to put those into a hash instead. }; undef %q; print "Generating $HOW_MANY keys\n"; my @keys; my $i = 0; while (@keys < $HOW_MANY) { $i++; my %hash = (0 => 0, $i => 0); if (%hash =~ /1/) { # Goes in the same bucket as 0 push @keys, $i; } } print "Putting the $HOW_MANY special keys into the hash.\n"; $i = 0; $lasttime = $s = time; foreach $key (@keys) { $h{$key} = 1; $i++; if (time() - $lasttime > 5) { my $h = %h + 0; print "I have put $i keys into the hash, using $h bucket(s)\n" ; $lasttime = time; } } $e2 = time - $s; print qq{ The $HOW_MANY special keys took $e2 seconds to put into the hash, instead of $e1 seconds. };

Update: (Much later.) Perl changed when it splits buckets so this program no longer runs slowly. Furthermore Perl's hashing algorithm is now random, so Dominus' program no longer runs slowly. Shucks.


In reply to Re: Re: Shift, Pop, Unshift and Push with Impunity! by tilly
in thread Shift, Pop, Unshift and Push with Impunity! by lhoward

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!
  • 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, 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2014-08-27 22:41 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (253 votes), past polls