Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: handler performance question

by hobbs (Monk)
on Feb 04, 2009 at 01:56 UTC ( [id://741165]=note: print w/replies, xml ) Need Help??


in reply to handler performance question

If the number of make/model pairs isn't likely to get too out-of-hand, you can also cache handles:
{ my (%handles, %did_mkdir); sub get_out_file { my ($make, $model) = @_; if (!defined $handles{$make}{$model}) { if (! $did_mkdir{$make} && ! -d "$out_dir/$make") { system "mkdir -p $out_dir/$make"; $did_mkdir{$make} = 1; } open $handles{$make}{$model}, "$out_dir/$make/$model" or die $!; } return $handles{$make}{$model}; } }
Then your main loop is reduced to:
while (<>) { my ($make, $model) = split /,/, $_, 4; my $out = get_out_file($make, $model); print $out $line; }
and all of the handles are closed at the end of execution. If this doesn't run you up against your filehandle limit, it will save you any number of unnecessary opens and mkdirs. If it does, well then you're going to have to start worrying about discarding entries from the cache, but you also have to ask yourself whether performance trumps complexity in that case. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found