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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

You have some problems with your code.    For example, you have created 55 variables, some of them duplicates, while I was able to write the same code with only 21 variables.    You use a hash to collect the file list and then copy its keys to an array when you only need the array.    You use lists of scalars when you could just use a single array.    You use three separate hashes when you could use a hash of arrays.

Here is my version of your code:

#!/usr/bin/perl use warnings; use strict; for my $files ( 1 .. 50 ) { $files *= 200; my @file_list; # list of files my %file_gene; for my $i ( 1 .. $files ) { open my $INF, '<', "$i.txt" or die "Cannot open '$i.txt' becau +se: $!"; chomp( my @g = <$INF> ); @{ $file_gene{ $i } }{ @g } = (); push @file_list, $i; } # overlapping percentage of file pairs # percentage of common items in file1 # percentage of common items in file2 my %file_pairs; my $time = time; my $pairs = 0; for my $i ( 0 .. $#file_list ) { my $m1 = $file_list[ $i ]; my ( @values, $pair ) = ( 0, 0, 0 ); for my $m2 ( @file_list[ $i + 1 .. $#file_list ] ) { ++$pairs; my @Nvalues = find_common_items( @file_gene{ $m1, $m2 }, 0 +.1 ); if ( $Nvalues[ 0 ] > $values[ 0 ] ) { $pair = $m1 . '_' . $m2; # file pair name @values = @Nvalues; } } if ( $pair ) { $file_pairs{ $pair } = \@values; } } $time = time - $time; print "$files files\t$pairs pairs\t$time sec\n"; } sub find_common_items { my ( $m1, $m2, $cutoff ) = @_; # find number of intersection items my $isn = grep exists $m2->{ $_ }, keys %$m1; # number of union items my $un = keys %{ { %$m1, %$m2 } }; # only store qualified file pairs my @values = ( 0, 0, 0 ); if ( $isn / $un > $cutoff ) { # For file pair m1_m2, overlapping percentage of file pairs = +intersection/union # For file pair m1_m2, percentage of common genes in file m1 = + intersection/size of file m1 # For file pair m1_m2, percentage of common genes in file m2 = + intersection/size of file m2 @values = ( $isn / $un, $isn / keys %$m1, $isn / keys %$m2 ) } return @values; }

In reply to Re: Why does Perl get slower when building a larger hash? (Not due to the memory swapping) by jwkrahn
in thread Why does Perl get slower when building a larger hash? (Not due to the memory swapping) by chialingh

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found