Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: TIMTOWTDI and other languages

by ariels (Curate)
on Apr 29, 2002 at 12:09 UTC ( #162810=note: print w/replies, xml ) Need Help??

in reply to TIMTOWTDI and other languages

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-08-13 20:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found