OK, I'm confused. I wanted to see how much faster BrowserUk's version was. But Benchmark is showing that it is slower.

#!/usr/bin/perl use warnings; use strict; use Benchmark qw( cmpthese ); my @chars = ('a'..'z', 'A'..'Z',0..9, qw[- _ ! & ? = ] ); open my $fh, ">", "/dev/null"; cmpthese( -20, { Original => sub { generateRandomFile($fh, 5) }, BrowserUk => sub {genFile($fh, 5) }, }); sub generateRandomFile { my( $gen_file_h, $final_size ) = @_; for (my $mbytes = 0; $mbytes < $final_size; $mbytes ++) { my $string=""; my @chars = split(" ","a b c d e f g h i j k l m n o p q r + s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z +- _ ! & ? = 0 1 2 3 4 5 6 7 8 9"); for (1..1048576) { my $rand = int(rand(68)); $string .= $chars[$rand]; } print $gen_file_h $string; } } sub genFile { my( $fh, $mb ) = @_; my $buf = chr(0) x 1024**2; for my $chi ( 1 .. $mb ) { substr $buf, $_, 1, $chars[ rand @chars ] for 0 .. 1024**2-1; print $fh $buf; } } __END__ s/iter BrowserUk Original BrowserUk 1.34 -- -15% Original 1.13 18% --

In reply to Re^4: Is rand() really that slow or am I just using it wrong? by Anonymous Monk
in thread Is rand() really that slow or am I just using it wrong? by adiuva

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.