in reply to my $var = ''; vs. use constant VAR => '';
my $const = \"Don't touch me!"; my $num = \5.6; local *const2 = \"Another string"; local *num2 = \3.14159; print "[$$const][$$num][$const2][$num2]\n"; ### try and modify it eval{ $num2 = 3; }; print $@; print "$num2\n";
Produces the following:#!/usr/bin/perl -w use Benchmark qw(timethese cmpthese countit timestr); use constant CONST_MOD => 3; sub CONST_SUB () { 3 }; my $const_my = 3; local $const_local = 3; my $const_ref = \3; local *const_alias = \3; print "mod[".CONST_MOD."]sub[".CONST_SUB."]\n" ."my[$const_my]local[$const_local]\n" ."ref[$$const_ref]alias[$const_alias]\n"; cmpthese (1_000_000, { CMod => sub { my $t = CONST_MOD }, CSub => sub { my $t = CONST_SUB }, CMy => sub { my $t = $const_my }, CLocal => sub { my $t = $const_local }, CRef => sub { my $t = $$const_ref }, CAlias => sub { my $t = $const_alias }, });
The only noticable differences are the my and the ref. The reference takes too long, the "my" is a little quicker but it is modifiable. All of the others are neck a neck, so, I'd vote for local *var=\23;mod[3]sub[3] my[3]local[3] ref[3]alias[3] Benchmark: timing 1000000 iterations of CAlias, CLocal, CMod, CMy, CRe +f, CSub... CAlias: 0 wallclock secs ( 0.79 usr + 0.00 sys = 0.79 CPU) @ 12 +65822.78/s (n=1000000) CLocal: 0 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12 +50000.00/s (n=1000000) CMod: 2 wallclock secs ( 0.80 usr + 0.00 sys = 0.80 CPU) @ 12 +50000.00/s (n=1000000) CMy: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13 +33333.33/s (n=1000000) CRef: 0 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 97 +0873.79/s (n=1000000) CSub: 0 wallclock secs ( 0.81 usr + 0.01 sys = 0.82 CPU) @ 12 +19512.20/s (n=1000000) Rate CRef CSub CLocal CMod CAlias CMy CRef 970874/s -- -20% -22% -22% -23% -27% CSub 1219512/s 26% -- -2% -2% -4% -9% CLocal 1250000/s 29% 3% -- -0% -1% -6% CMod 1250000/s 29% 3% 0% -- -1% -6% CAlias 1265823/s 30% 4% 1% 1% -- -5% CMy 1333333/s 37% 9% 7% 7% 5% --
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: my $var; vs. use constant VAR = '';
by MeowChow (Vicar) on Apr 27, 2001 at 02:50 UTC | |
by Rhandom (Curate) on Apr 27, 2001 at 03:05 UTC | |
by MeowChow (Vicar) on Apr 27, 2001 at 03:23 UTC | |
by Rhandom (Curate) on Apr 27, 2001 at 03:33 UTC | |
by donaldhasson (Initiate) on Aug 03, 2008 at 16:54 UTC |