Re: Is there any performance benefit to...



On Thu, 25 Aug 2005 14:24:52 +0100, Derek Fountain wrote:
> I've inherited a piece of code that does something like this:
>
> sub func {
> my %hash = ();
>
> ...lots of code that populates and uses the hash
>
> %hash = ();
> }
>
> There are hundreds of these functions and they are called millions of
> times in a procedure which takes hours, sometimes days, to complete. The
> guy who wrote it was obviously concerned about performance.

Have you asked him?

I would have said it was pointless myself, and if anything likely to
increase running time, but having written before that even the best
programmers are lousy at guessing about performance optimizations, I
didn't guess:

[peter@tweety ~]$ cat /tmp/foo
#!/usr/bin/perl
use strict;
use warnings;

use Benchmark qw(cmpthese);

cmpthese(20,
{ clear => sub { my %h = map {$_,$_} 1..1E5; %h = () },
no_clear => sub { my %h = map {$_,$_} 1..1E5 }
}
);
[peter@tweety ~]$ /tmp/foo
s/iter no_clear clear
no_clear 2.29 -- -3%
clear 2.22 3% --

Go figure.

[peter@tweety ~]$ perl -v
This is perl, v5.8.5 built for i386-linux-thread-multi

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

.