in reply to Is rand() really that slow or am I just using it wrong?

Most of your time is being spent building an 8-char string from 8 random bytes, then copying that to a 16-byte string and adding 8 more random bytes; then copying that to a 32-byte string and adding 16 more random bytes; then copying that to a 64-byte string ...

Try this version:

#! perl -slw use strict; my @chars = ('a'..'z', 'A'..'Z',0..9, qw[- _ ! & ? = ] ); sub genFile { my( $fh, $mb ) = @_; my $buf = chr(0) x 1024**2; for( 1 .. $mb ) { substr $buf, $_, 1, $chars[ rand @chars ] for 0 .. 1024**2-1; print $fh $buf; } }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Is rand() really that slow or am I just using it wrong?
by adiuva (Sexton) on Aug 08, 2013 at 18:56 UTC
    Thanks for the fast reply! But even though your code looks a lot nicer than mine it's not faster. :(
    If you generate a 10MB file you can still see 1MB chunks being written to the harddisk, while using the sub superfast() the file is there instantly.
      it's not faster

      It *is* faster than your method.

      Not as fast as writing 10MB of a single character for sure, but then you are calling a function, rand 10 million times; it has to be slower.

      The only question is; how much faster do you need it to be?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        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% --