If you're after a "best" implementation ... tie::file

There's a couple of problems with that.

  1. Performance:

    Writing a file with Tie::File, even with memory allocated to easily accommodate the whole file, is orders of magnitude slower than direct writing.

    c:\test>junk7 -N=10e3 ### 1/2 MB Took 0.098 seconds Took 34.255 seconds c:\test>junk7 -N=20e3 ### 1 MB Took 0.197 seconds Took 137.506 seconds c:\test>junk7 -N=1e6 ### 50 MB Took 9.449 seconds ^C

    By the time you get to 50 MB I estimate it will take hours instead of 10 seconds.

  2. There doesn't seem to be any simple way to binmode a Tie::File tied file. Which means that on some systems, the data in the file will be different to that checksummed:
    21/07/2010 01:26 510,033 junk.dat 21/07/2010 01:26 520,034 junk2.dat

Test code:

#! perl -slw use strict; use Time::HiRes qw[ time ]; use Tie::File; use Digest::MD5 qw[ md5_hex ]; our $N //= 1e6; my $start = time; open OUT, '+>:raw', 'junk.dat'; print OUT md5_hex( 0 ); my $data = 'x' x 50; my $md5 = new Digest::MD5; for ( 1.. $N ) { print OUT $data; $md5->add( "$data\n" ); } seek OUT, 0, 0; print OUT $md5->hexdigest; close OUT; printf "Took %.3f seconds\n", time-$start; $start = time; tie my @lines, 'Tie::File', 'junk2.dat', memory => 52 * $N; $md5 = new Digest::MD5; push @lines, md5_hex( 0 ); for ( 1.. $N ) { push @lines, $data; $md5->add( "$data\n" ); } $lines[ 0 ] = $md5->hexdigest; untie @lines; printf "Took %.3f seconds\n", time-$start;

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.
RIP an inspiration; A true Folk's Guy

In reply to Re^2: Design/Style question about writing to a file from different functions by BrowserUk
in thread Design/Style question about writing to a file from different functions by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.