On 2001-09-13 17:01:20 -0400, Rob Ransbottom wrote:
> On Thu, 13 Sep 2001 [EMAIL PROTECTED] wrote:
>
> > I have a minor optimization suggestion. Instead of this:
>
> > unless ($sth_routine_name) {
> > #setup statement handle
> > }
>
> > Do this:
> >
> > $sth_routine_name ||= $dbh->prepare(....);
>
> > pretty sure this is more efficient than setting up the unless block.
First, I should say that I think that the speed differences between
these methods are IMHO negligible in almost all situations and that one
should use the most readable version (I like ||=, btw. It looks neat).
> True, these are about the same, but in order of speed:
>
> $i ||= 1;
> $i = 1 unless $i;
> $i = $i ? 1 : 0;
>
> All faster than:
>
> unless ( $i) { $i = 0;}
Just for fun I tried that with perl 5.6.0 under Linux on a Pentium
II/233, and unless ( $i) { $i = 1; } came out fastest (yes, I changed 0
to 1 here. Otherwise it would not be equivalent to $i ||= 1):
Rate ?: post unless ||= ||= do pre unless
?: 1440922/s -- -53% -57% -76% -80%
post unless 3086420/s 114% -- -7% -49% -57%
||= 3333333/s 131% 8% -- -45% -53%
||= do 6024096/s 318% 95% 81% -- -16%
pre unless 7142857/s 396% 131% 114% 19% --
here is the script:
#!/usr/local/bin/perl -w
use strict;
use Benchmark qw/cmpthese/;
my $i = 1;
cmpthese(5000_000, {
'||=' => sub { $i ||= 1 },
'||= do' => sub { $i ||= do { 1 } },
'post unless' => sub { $i = 1 unless $i },
'pre unless' => sub { unless ($i) { $i = 1 } },
'?:' => sub { $i = $i ? $i : 1 },
}
);
I don't trust the Benchmark module, though. It reported one negative
"wallclock secs" value at every run, and the wallclock and usr times
differ too much for a test which should essentially take 100% user time.
hp
--
_ | Peter J. Holzer | My definition of a stupid question is
|_|_) | Sysadmin WSR / LUGA | "a question that if you're embarassed to
| | | [EMAIL PROTECTED] | ask it, you stay stupid."
__/ | http://www.hjp.at/ | -- Tim Helck on dbi-users, 2001-07-30
PGP signature