I disagree. You can do random access with ASCII so long as you write fixed length records, but it does take about 50% longer to do the conversion. It's the conversion that chews up most of the time, so pack() can be a substantial time saver.

In the test below, writing floats is 10 times faster than writhing ascii. The file is half the size, so that smaller size only accounts for a factor of 2 speed-up. A factor of 5 comes from not doing the ascii conversion.

use warnings; use strict; use Time::HiRes qw( gettimeofday tv_interval ); my $size = 1000; my $last_idx = $size-1; my $float_value; my $t0; $t0 = [gettimeofday]; open ASC, '>', "asciinum.txt" or die "can't open ascii file"; for my $row (0..$last_idx) { my @result = (); my $row_len = $row+1; $float_value = $row/10_000 + 1/100_000_000; for my $col (0..$last_idx) { #$float_value = $row/10_000 + $col/100_000_000; push @result, $float_value; } print ASC join(',', @result); } print STDERR "Ascii write took -- ",tv_interval ( $t0 ),"\n"; close ASC; $t0 = [gettimeofday]; open FLOAT, '>', "floatnum.txt" or die "can't open float file"; for my $row (0..$last_idx) { my @result = (); my $row_len = $row+1; $float_value = $row/10_000 + 1/100_000_000; for my $col (0..$last_idx) { #$float_value = $row/10_000 + $col/100_000_000; push @result, $float_value; } print FLOAT pack("F$row_len", @result); } print STDERR "Float write took -- ",tv_interval ( $t0 ),"\n"; close FLOAT; # Gave the following results on my fairly slow machine, with $size = 1 +000 # Ascii write took -- 15.078125 # Float write took -- 2.778736

In reply to Re^2: Issue on covariance calculation by rodion
in thread Issue on covariance calculation by Mandrake

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.