in reply to Re^3: Is rand() really that slow or am I just using it wrong?
in thread Is rand() really that slow or am I just using it wrong?
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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Is rand() really that slow or am I just using it wrong?
by BrowserUk (Patriarch) on Aug 09, 2013 at 14:46 UTC |