in reply to Hashes/Scalars and Memory Usage

Here are three benchmarks and some cheezy benchmarking code for a hash, an array, and a variable. The access times are hardly very different. Variables are the fastest across the board, but if you are using variables to 'improve' your code I think your time is better spent looking at other things, like regexes, rather than trying to use only vars. Also, your code, with so many variables, may appear unreadable.
Benchmark: timing 1000000 iterations of hsh, reg, var... hsh: 0 wallclock secs ( 1.25 usr + 0.00 sys = 1.25 CPU) @ 79 +8084.60/s (n=1000000) reg: 2 wallclock secs ( 0.85 usr + 0.00 sys = 0.85 CPU) @ 11 +75088.13/s (n=1000000) var: 1 wallclock secs ( 0.77 usr + 0.00 sys = 0.77 CPU) @ 12 +97016.86/s (n=1000000) Benchmark: timing 1000000 iterations of hsh, reg, var... hsh: 2 wallclock secs ( 1.17 usr + 0.00 sys = 1.17 CPU) @ 85 +2514.92/s (n=1000000) reg: 1 wallclock secs ( 0.85 usr + 0.01 sys = 0.86 CPU) @ 11 +61440.19/s (n=1000000) var: 0 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13 +31557.92/s (n=1000000) Benchmark: timing 1000000 iterations of hsh, reg, var... hsh: 2 wallclock secs ( 1.18 usr + 0.00 sys = 1.18 CPU) @ 84 +5308.54/s (n=1000000) reg: 1 wallclock secs ( 0.85 usr + 0.00 sys = 0.85 CPU) @ 11 +75088.13/s (n=1000000) var: 1 wallclock secs ( 0.77 usr + 0.00 sys = 0.77 CPU) @ 12 +97016.86/s (n=1000000) #!/usr/bin/perl -w use strict; use Benchmark; my @foo = (1 .. 30); my %foo; my $foo = 10000; for (1.30) { $foo{$_} = $_; } timethese(1000000,{ var=>q( my $v = $foo; ) , reg=>q( my $v = $foo[30];), hsh=>q( my $v = $foo{'30'};) }); 1;

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re: Re: Hashes/Scalars and Memory Usage
by Fastolfe (Vicar) on Feb 09, 2001 at 00:52 UTC
    Perl is doing an unseen optimization here. Since you're working with a variable with its name spelled out in the code, Perl knows immediately what scalar you are wanting to access, without having to do much in the way of symbol table lookups. I think the original poster was wanting to know the difference between setting and accessing a scalar in a hash versus a soft reference to a variable, the name of which might not be known. Here's some Benchmark code that takes that into consideration. These results are closer to what I expect, but I'm actually surprised that soft references performed this poorly. Note that I may easily be mistaken about the original poster's requirements here (named variable versus a soft reference), but I think with both of our data, there's enough information to see which is more efficient. Regardless, we're talking about an operation that takes a miniscule amount of time. If we save a few microseconds of execution time, that doesn't add up to anything significant, ever.
    Benchmark: running array, hash, softref, each for at least 10 CPU seco +nds... array: 11 wallclock secs (10.50 usr + 0.00 sys = 10.50 CPU) @ 32 +119.33/s (n=337253) hash: 11 wallclock secs (10.30 usr + 0.00 sys = 10.30 CPU) @ 22 +763.50/s (n=234464) softref: 10 wallclock secs (10.39 usr + 0.00 sys = 10.39 CPU) @ 17 +753.61/s (n=184460) use Benchmark; foreach (1..10) { $hash{$_} = 'x'; $array[$_] = 'x'; ${"var_$_"} = 'x'; } sub hash { $hash{$_} = 'hash!' foreach 1..10 } sub array { $array[$_] = 'array!' foreach 1..10 } sub softref { ${"var_$_"} = 'ref!' foreach 1..10 } timethese(-10, { hash => \&hash, array => \&array, softref => \&softref, });