Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Perl Code Runs Extremely Slow

by samtregar (Abbot)
on Jun 13, 2006 at 20:06 UTC ( #555116=note: print w/replies, xml ) Need Help??

in reply to Perl Code Runs Extremely Slow

There are so many performance problems in this code that it's kind of hard to know where to begin! Here's a few that jump out right away:

  • Don't open file 2 for each line of file 1 and read through every line! If there are 1 million lines in file 1 and 500 thousand lines in file 2 then you'll read 500 billion lines from file 2! Instead read file 2 once and re-use the hash for each lookup.
  • Don't re-sort all the keys from file 1 everytime you read a line from file 1. (UPDATE: Looking again I see that %fets is actually local to the while(). Why are you using a hash at all here? Why are you calling sort() when only one key is present?)
  • You may not have enough memory to actually hold all of file 2 in memory at once. If you don't you'll run into swap, which will be slow no matter what you do. You can fix this by storing the hash in a database file via DB_File or something similar.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://555116]
[erix]: or compile & run an instance as yourself (not user 'postgres')
[erix]: easiest setup is to run with PG* env variables set, and a custom .pgpass files (can be a $PGPASSFILE)
[Corion]: Hmm - "trust" sounds interesting, but I have still to find whether I can restrict that on a per-DB level ;)
[Corion]: erix: Sure, but SQLite doesn't even need that :)
[hippo]: Corion: Yes, you can. It's all in the pg_hba.conf
[erix]: yeah, but there is a lot SQLite cannot do :)
[hippo]: Database is the 2nd column.
shmem .oO( docker. docker. docker? docker. docker! docker )
[shmem]: at least, some Ook.
[erix]: another simple Pg thing is to running a database off a tsv (alas, readonly)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2018-02-23 15:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (302 votes). Check out past polls.