Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

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

If you have thousands of lines to process this may be quicker than some other approaches. It will process 100,000 lines (of 2 to 4 elements each) in under 4 seconds.

I won't attempt to categorise the algorithm because it relies on using hashes for lookups, and there is always the argument about whether they are O(1) or 0(logN).

Update: On emperical evidence this is O(N) (at ~4secs/100,000) upto the limit of memory.

#! perl -slw use strict; our $LINES ||= 1000; ## Gen some test data to a simulated file my @a = qw[ one two three four five six seven eight nine ];; my @lines = map{ join '_', @a[ map{ rand @a } 1 .. 2+rand 3 ] } 1 .. $ +LINES; warn "Processing " . @lines . " start: " . localtime; ## Index the lines my %index; for my $idx ( 0 .. $#lines ) { $index{ $_ }{ $idx }++ for split '_', $lines[ $idx ]; } my @keys = keys %index; while( my $first = pop @keys ) { for my $second ( @keys ) { print "$first/$second: ", join "\n\t", @lines[ grep{ exists $index{ $second }{ $_ } } keys %{ $index{ $first } } ]; } } warn "Processing " . @lines . " stop: " . localtime; __END__ P:\test>392861 -LINES=100000 >nul Processing 100000 start: Wed Sep 22 19:58:46 2004 at P:\test\392861.pl + line 10. Processing 100000 stop: Wed Sep 22 19:58:50 2004 at P:\test\392861.pl + line 31.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

In reply to Re: nested combinations: algorithm advice? by BrowserUk
in thread nested combinations: algorithm advice? by revdiablo

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 meditating upon the Monastery: (2)
As of 2024-04-25 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found