For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2005 > Create a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Create a file
M. Lewis

2005-10-25, 3:55 am

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;


--

Avoid the Fortran arithmetic IF (or better yet, just avoid Fortran).
00:45:04 up 3 days, 7:56, 5 users, load average: 1.92, 1.58, 0.92

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

2005-10-25, 3:55 am

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;
}



John
--
use Perl;
program
fulfillment
M. Lewis

2005-10-25, 9:55 pm


Jay Savage wrote:
> On 10/25/05, John W. Krahn <krahnj@telus.net> wrote:
>
>
>
> 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com