Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: my $var; vs. use constant VAR = '';

by Rhandom (Curate)
on Apr 27, 2001 at 01:55 UTC ( [id://75935]=note: print w/replies, xml ) Need Help??


in reply to my $var = ''; vs. use constant VAR => '';

Something that will let you use scalars but will prevent them being modified:
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";

The first ones are not as pretty as dollar variables, and the second have a slight overhead of local, but I think that should give you some nice unmodifiable variables.

UPDATE:
Benchmarks are always good. Read em and weep:
#!/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 }, });
Produces the following:
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% --
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;

my @a=qw(random brilliant braindead); print $a[rand(@a)];

Replies are listed 'Best First'.
Re: Re: my $var; vs. use constant VAR = '';
by MeowChow (Vicar) on Apr 27, 2001 at 02:50 UTC
    Ah, the perils of benchmarking... my results from v5.6.1 on Linux:
    Rate CRef CAlias CMy CLocal CMod CSub CRef 2702703/s -- -8% -22% -22% -24% -27% CAlias 2941176/s 9% -- -15% -15% -18% -21% CMy 3448276/s 28% 17% -- -0% -3% -7% CLocal 3448276/s 28% 17% 0% -- -3% -7% CMod 3571429/s 32% 21% 4% 4% -- -4% CSub 3703704/s 37% 26% 7% 7% 4% --
    and then the same program, run just a second later:
    CAlias 2857143/s -- -0% -3% -6% -20% -23% CRef 2857143/s 0% -- -3% -6% -20% -23% CMod 2941176/s 3% 3% -- -3% -18% -21% CSub 3030303/s 6% 6% 3% -- -15% -18% CLocal 3571429/s 25% 25% 21% 18% -- -4% CMy 3703704/s 30% 30% 26% 22% 4% --
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      Don't worry, I had that happen on mine as well, but in every case, the my was a percentage faster and the ref was a percentage slower and everything else just hummed around in the middle. The point wasn't the benchmark. The point was that declaring aliases or setting up your own constant subs, are not that much slower than my'ing a variable or local'ing a variable.

      The point of benchmark is ballpark, and all of the types are in the ball park.

      my @a=qw(random brilliant braindead); print $a[rand(@a)];
        Interestingly, if you throw a literal value in the mix, as done below, you will find that the literal is even usually a hair slower than a my variable. I'd be very curious to know why that is:
        #!/usr/bin/perl -w use strict; use vars qw(*const_alias $const_local); use Benchmark qw(cmpthese); local *const_alias = \100; my $const_ref = \100; local $const_local = 100; my $const_my = 100; sub CONST_SUB () { 100 }; use constant CONST_MOD => 100; print "mod[".CONST_MOD."]\nsub[".CONST_SUB."]\n" ."my[$const_my]\nlocal[$const_local]\n" ."ref[$$const_ref]\nalias[$const_alias]\n"; # run iteratively within the sub to get a less skewed # result - that's my theory anyway :-) my $iter = 500; my $t; cmpthese (-5, { CMod => sub { $t = CONST_MOD for 1..$iter }, CSub => sub { $t = CONST_SUB for 1..$iter }, CMy => sub { $t = $const_my for 1..$iter }, CLocal => sub { $t = $const_local for 1..$iter }, CRef => sub { $t = $$const_ref for 1..$iter }, CAlias => sub { $t = $const_alias for 1..$iter }, CLit => sub { $t = 100 for 1..$iter }, }); ### RESULTS (repeatable this time :) ### Rate CRef CAlias CLocal CSub CLit CMod CMy CRef 3735/s -- -9% -9% -10% -11% -11% -13% CAlias 4088/s 9% -- -0% -2% -3% -3% -5% CLocal 4088/s 9% 0% -- -2% -3% -3% -5% CSub 4172/s 12% 2% 2% -- -1% -1% -3% CLit 4208/s 13% 3% 3% 1% -- -0% -2% CMod 4210/s 13% 3% 3% 1% 0% -- -2% CMy 4293/s 15% 5% 5% 3% 2% 2% --
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print

        So here are my benchmark results on Win XP with version 5.10. Mine are drastically different showing the 'use constant' is fastest. Do the newer versions of Perl treat constants differently?
        mod[3]sub[3] my[3]local[3] ref[3]alias[3] Rate CAlias CRef CLocal CMy CSub CMod CAlias 3014249/s -- -9% -20% -22% -27% -45% CRef 3305288/s 10% -- -12% -14% -20% -40% CLocal 3749148/s 24% 13% -- -3% -10% -32% CMy 3849294/s 28% 16% 3% -- -7% -30% CSub 4149204/s 38% 26% 11% 8% -- -25% CMod 5504587/s 83% 67% 47% 43% 33% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-23 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found