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. HTH, -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!