in reply to Re: 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?

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.
  • Comment on Re^2: Is rand() really that slow or am I just using it wrong?

Replies are listed 'Best First'.
Re^3: Is rand() really that slow or am I just using it wrong?
by BrowserUk (Patriarch) on Aug 08, 2013 at 21:51 UTC
    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% --
        OK, I'm confused.

        You're not confused; you are correct!

        I don't know what I did different late last night, but retrieving the same command lines from my console history I used to test this yesterday, that showed my way taking almost half the time, today show it taking ~15% longer.

        So, my apologies to the OP and thank you Mr Anonymonk for checking me.

        By way of recompense I offer this version which takes 1/4 of the time, at the cost of being very slightly less fair:

        my @chars = ('a'..'z', 'A'..'Z',0..9, qw[- _ ! & ? = ] ); sub genFile { my( $fh, $mb ) = @_; my @buf = ( @chars ) x 61; $#buf = 4096; for( 1 .. $mb * 256 ) { print $fh join'', shuffle @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.