Pagoda wrote:

> Can I improve the performance of script by using constant?
> 
> Which is the better one?
> 
> use constant const => 1e-12
> 
> or
> 
> my $const = 1e-12
> 

the current implmentation of constant is that when you say:

use constant const => 1e-12;

it's basically translated into:

sub <caller>::const(){
        1e-12;
}

where <caller> is replaced by the caller's package. so for example:

#!/usr/bin/perl -w
use strict;

use constant const => 1e-12;

print const;

__END__

is bascially translated into:

#!/usr/bin/perl -w
use strict;

sub main::const(){
        1e-12;
}

print const;

__END__

which when Perl's optimizer comes in, translates it into:

#!/usr/bin/perl -w
use strict;

print 1e-12;

__END__

that's how you achive the constantness of a variable and a bit of 
performance increase. it has roughly the same affact as saying:

[panda]# perl -MO=Deparse -e 'sub const(){1e-2} print const'
print 0.01;
-e syntax OK

notice the function disappear. i would recommand that you use constant 
whenever possible regardless of whether it really makes your code goes 
faster because of the gain of readability.

btw, you statment:

my $const = 1e-21;

is not a const at all. it's just a scalar.

david
-- 
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s

,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to