Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Huge data file and looping best practices

by BrowserUk (Patriarch)
on Apr 26, 2009 at 23:04 UTC ( [id://760218]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Huge data file and looping best practices
in thread Huge data file and looping best practices

Would it be worthwhile to pre-allocate the @patNos and @data arrays with $#array = N_000_000; statements?

It wouldn't hurt, but would make little difference to the overall runtime given the scale of the processing.

Even though it would lead to 8,000,000 redundant entries giving the variance of each patent with itself (i.e., 0), might it be profitable to eliminate the next if $first == $second; statement from the inner loop of the variance calculation section? Or, avoiding redundant entries, to break the inner loop into two consecutive loops with identical BLOCK contents: for my $second (0 .. $first-1) { ... } and then for my $second ($first+1 .. $#patNos) { ... } ?

With time to give it a little more thought, there is no point in comparing both patent 1 with patent 2 & patent 2 with patent 1. With that insight, the inner loop should run from $first +1 .. $#patNos - 1. That reduces the variance calculations from 64e12 to 32e12 - 8e6 for a further halving of the runtime:

#! perl -sw use 5.010; use strict; my( $i, @patNos, @data ) = 0; $#patNos = $#data = 8e6; while( <> ) { my @bits =split ','; $patNos[ $i ] = shift @bits; $data[ $i++ ] = pack 'b480', @bits; print "\r$.\t" unless $. % 1000; } say "\n", time; open OUT, '>', 'variances.csv' or die $!; my @variances; for my $first ( 0 .. $#patNos-1 ) { for my $second ( $first+1 .. $#patNos ) { say OUT "$patNos[ $first ], $patNos[ $second ], ", unpack '%32b*', ( $data[ $first ] ^ $data[ $second ] ); } } close OUT;

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://760218]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-16 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found