Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

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

Doing it in C and with moderate performance requirements (e.g. ~100_000 elements in each list), I'd go samtregar's way and compare both lists sorted (this is probably faster than sorting the one and binary-searching the other through it when both lists have lengths of the same order).

Doing it in C, but in a high performance situation, I'd try to change the data representation. If we can recast the problem such that the lists can be stored as bit strings, nothing can beat an AND. On a modern processor I can compare 64 elements at a time; unroll the loop 4--8 times and you'll be running about as fast as is possible.

If elements of the lists are integers in a known range, this method is obviously suitable. In other cases, it might be possible to use a hash table (probably called a "symbol table") to translate list elements into indices, and then represent each list as a bitmap.

Note that this approach might or might not be suitable, depending on the precise character of the code. Unfortunately, high-performance solutions often suffer from this problem.

samtregar's solution is also useful in another advanced programming environment. In a shell script, to get common elements of files <samp>AAA</samp> and <samp>BBB</samp> efficiently and in sorted order, try

sort -u AAA > AAA.sorted sort -u BBB > BBB.sorted comm -12 AAA.sorted BBB.sorted > common
This technique works amazingly well, in fact.


In reply to Re: TIMTOWTDI and other languages by ariels
in thread TIMTOWTDI and other languages by Ovid

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 pondering the Monastery: (4)
As of 2024-04-18 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found