Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: How to remove duplicates from a large set of keys

by nite_man (Deacon)
on Feb 10, 2005 at 08:46 UTC ( [id://429626]=note: print w/replies, xml ) Need Help??


in reply to Re: How to remove duplicates from a large set of keys
in thread How to remove duplicates from a large set of keys

Unfortunately, lookup in the hash with more than million keys takes a lot of time. I suspect that there is a way to do it more efficient.

---
Michael Stepanov aka nite_man

It's only my opinion and it doesn't have pretensions of absoluteness!

  • Comment on Re^2: How to remove duplicates from a large set of keys

Replies are listed 'Best First'.
Re^3: How to remove duplicates from a large set of keys
by demerphq (Chancellor) on Feb 10, 2005 at 08:50 UTC

    Lookup in a hash of 1 million keys is rougly the same as lookup in a hash of 10 keys. :-) (Assuming you are still inside of physical memory.)

    Its creating the hash thats the problem. It takes a long time, especially if you dont know how many records you are storing up front.

    ---
    demerphq

      Lookup in a hash of 1 million keys is rougly the same as lookup in a hash of 10 keys
      Can you explain, please

      ---
      Michael Stepanov aka nite_man

      It's only my opinion and it doesn't have pretensions of absoluteness!

        Hash lookup in the type of hash structure used in perl is O(1). Its basically the time to calculate the hash value of the key, which is not dependent on the size of the hash, followed by the application of a bitmask to obtain an index into an array of linked lists, which are normally very short, followed by a key comparison on each element in the LL until the actual key is found or the end of the LL is reached. Under non pathological circumstances the LL should hold 0 or 1 elements. The end result is that the lookup time (memory swapping aside) is O(1) and thus independent of the number of keys in the hash.

        Its true that under pathological cirumstances you could end up with a bucket with a million keys in it, but Perl has a number of heuristics to prevent this happening in practice. Building the hash is more expensive because Perl cannot know the final size required and must always have a power of two number of buckets, so while growing the hash array needs to be expanded and the keys remapped which costs time. But its purely on storage. Once built a perl hash should behave in O(1) time, or rather time proportional to the length of the key being looked up.

        ---
        demerphq

Re^3: How to remove duplicates from a large set of keys
by jbrugger (Parson) on Feb 10, 2005 at 08:59 UTC
    Is it?
    This script took 6 seconds to complete (run), with 2 million keys, on a Celeron 2.4 Ghz, 512 Mb, running X, Mozilla, Apache, john, etc. etc. and took at max. 27 % of the memory
    #!/usr/bin/perl -w use strict; my %lookupTable; for (my $i=0; $i<=2000000; $i++) { if (!$lookupTable{$i}) { $lookupTable{$i} = 1; } }

      I was curious so I took the same code, added a timer, and ran it on win32. It took 10 seconds. However, I'm running a 1.8 Pentium M, 1G of memory, WinXP, ActivePerl 5.8.6.

      I suspect that nite_man does not have as beefy of a box as you do. It might be acceptable if the look up doesn't occur frequently.

      In retrospect, it wasn't a good test since the system specs weren't the same and I'm not fimilar with the whole problem. But it was a good 30 second distraction. :)

      For the curious:

      #!perl -w use strict; my $startTime = time(); my %lookupTable; for (my $i=0; $i<=2000000; $i++) { if (!$lookupTable{$i}) { $lookupTable{$i} = 1; } } my $totalTime = time() - $startTime; print "Total Time: $totalTime\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-10 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found