Well, Mike, while the second Anonymous Monk in this thread was a tad heavy on snideness and trivial complaints, the truth remains that there is a lot more wrong with your code (and your post) than the problem that you sort of describe ("too slow, memory leak").
How big are the data files, and how many files (i.e. how much data, total)? If it's a lot, well, maybe it takes so long simply because there's a lot of data and you are reading it all twice. For that matter, how much elapsed time do you consider to be "slow"? How much faster do you think it should be? (update: and what sort of system are you running on?)
Based on what you've said and shown, I would guess the following:
- There's no evidence here of any sort of "memory leak" or using too much memory, because the code as shown is not keeping anything in memory (except a handful of scalar values).
- You are assigning the value returned by each "read()" call to a variable, but you never check the value. This is just one symptom of a rampant lack of error checking.
- There's no need to read the file twice; whatever you're doing in the second loop over the file, just do that in the first loop instead. It seems like the only information you derive from the first pass is the number of records (quaintly referred to as "$x"), and you don't use that information in the second loop for anything (except to say how many iterations to in reading the file -- but that's no different from using "eof" in the first loop).
- You probably don't need to use "sleep(1)" -- maybe that's what's slowing it down? ;-)
Anyway, why not print some useful messages to STDOUT -- show the current time before reading the file, show it after reading the file, show it again at some other point after you've done some other major processing step, and so on. It's even quite simple to show the elapsed time in seconds:
$start = time;
# do one big step
$now = time;
printf "Step one took %d sec\n", $now - $start;
$start = $now;
# do next big step
$now = time;
printf "Next step took %d sec\n", $now - $start'
# and so on...
That could tell you where most of the time is going, so you'll know which part of the code is taking the longest.
(update: fixed typo in last printf statement)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.