Jay Savage wrote:
On 10/25/05, John W. Krahn <[EMAIL PROTECTED]> wrote:

M. Lewis wrote:

I'm trying to create a file of a certain size. Specifically 15MB for
some testing purposes. I want this file to have random characters. I'm
sure it would be easier (faster) to use the same character. What I'm
finding is my code is terribly slow to create a 15MB file.

Is there a better / more efficient way ?

Thanks,
Mike


#!/usr/bin/perl

use strict;
use warnings;

#PURPOSE - Create a file of a certain size (random characters)

my $file = './test.txt';
my $size = 15728640;       # 15MB

open (OUT,">$file") || die "Cannot open $file :$!";

print OUT (map+chr(ord('a')+rand 26),1..$size);

close OUT;

Your program is really slow because you are creating a 15MB list in memory.
This will be a lot faster:

my $size = 1024;

for ( 1 .. 15 * 1024 ) {
   print OUT map chr( ord( 'a' ) + rand 26 ), 1 .. $size;
   }


Why map at all? Just forget the list entirely and let the system
buffer the output however it wants; it's going to anyway:

    print OUT chr( ord( 'a') + rand 26 ) for 1..(15*1024*1024);

This is about 30% faster than map, at least for me. YMMV, of course.


Thanks very much both John and Jay. Both solutions were better (and certainly faster) than mine. It's not that I'm going to be running this often. It's more important to learn the correct / most efficient way of doing it.

Benchmark: timing 5 iterations of jay, john...
jay: 66 wallclock secs (65.00 usr + 0.55 sys = 65.55 CPU) @ 0.08/s (n=5) john: 104 wallclock secs (101.16 usr + 0.81 sys = 101.97 CPU) @ 0.05/s (n=5)

Thanks again,
Mike

--

 And on the seventh day, He exited from append mode.
  21:25:02 up  9:24,  4 users,  load average: 0.57, 0.19, 0.18

 Linux Registered User #241685  http://counter.li.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to