kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

In the following code to write the contents of a file to a specified filename, the buffer size is given as 16_384 (from this node Re: File Upload - What Next?):

use CGI: my $q = new CGI; use constant BUFFER_SIZE => 16_384; my $filename = $q->param( "filename" ) || die; # Assuming $filename is sanitized... my $fh = $q->upload( "file" ); my $buffer = ''; open(OUT,">/tmp/$filename"); binmode $fh; binmode OUTPUT; # Write contents to output file while ( read( $fh, $buffer, BUFFER_SIZE ) ) { print OUTPUT $buffer; }
I've seen other numbers such as 1024 (1 kb). Is it an arbitrary number of bytes?

Thanks in advance :)

Replies are listed 'Best First'.
Re: Ideal buffer size..
by waswas-fng (Curate) on Apr 22, 2004 at 04:00 UTC
    A buffer size equal to or a multiple of your filesystem block size is optimal (assuming your filesystem block size is optimal for the storage type/ stripe size etc on the raw device).


    -Waswas
      Thansk, waswas-fng!

      Is there a generic size that fits all?

      :)

        For this particular case, any number would work fine really. If you want to be part of the "in-crowd", just use a multiple of 1024 (the 16384 you see in that code is 1024*16). It's really up to you though... depends on how much memory you want to use up at any given time.

        Of course not :)

        But most OS's seem to use 4096 bytes (that is, four kilobytes), so you should be OK with that.