in reply to Symbolic refs aka. dynamic variables again

i think your benchmark is a little off, since it's comparing global scalars to a lexical hash. hopefully this one's more accurate (but i'm no expert.)

Update:

here, global vars win in speed, as you quoted. i did not test memory requirements. of course, i meant that lexicals win... what was i looking at when i typed that?

#!perl use Benchmark; my %l_hash; sub use_global_hash { ++$hash{ 'foo' . $_ } for 0 .. 1000 } sub use_global_vars { ++${ 'foo' . $_ } for 0 .. 1000 } sub use_lexical_hash { ++$l_hash{ 'foo' . $_ } for 0 .. 1000 } timethese -10, { use_global_hash => \&use_global_hash, use_global_vars => \&use_global_vars, use_lexical_hash => \&use_lexical_hash, }; __END__ > t-b-symref.pl Benchmark: running use_global_hash, use_global_vars, use_lexical_hash +for at least 10 CPU seconds... use_global_hash: 11 wallclock secs (10.58 usr + 0.00 sys = 10.58 CPU) + @ 744.83/s (n=7884) use_global_vars: 10 wallclock secs (10.54 usr + 0.02 sys = 10.56 CPU) + @ 561.35/s (n=5925) use_lexical_hash: 11 wallclock secs (10.65 usr + 0.00 sys = 10.65 CPU +) @ 794.74/s (n=8460)

~Particle *accelerates*

Replies are listed 'Best First'.
Re: Re: Symbolic refs aka. dynamic variables again
by diotalevi (Canon) on Apr 25, 2003 at 02:15 UTC

    Nah - your results are way too close. On slower hardware lexicals are clearly fastest, globals come in second and symbolic refs in last place. This is all easily explained in terms of involved opnodes. Symbolic refs add a handful of nodes, and globals are one opnode heavier than lexicals.

    Benchmark: running use_global_hash, use_global_vars, use_lexical_hash, + each for at least 10 CPU seconds... use_global_hash: 11 wallclock secs (10.45 usr + 0.00 sys = 10.45 CPU) + @ 112.63/s (n=1177) use_global_vars: 11 wallclock secs (10.55 usr + 0.00 sys = 10.55 CPU) + @ 83.32/s (n=879) use_lexical_hash: 11 wallclock secs (10.48 usr + 0.00 sys = 10.48 CPU +) @ 120.71/s (n=1265) Rate use_global_vars use_global_hash use_lexical_ +hash use_global_vars 83.3/s -- -26% +-31% use_global_hash 113/s 35% -- + -7% use_lexical_hash 121/s 45% 7% + --
Re: Re: Symbolic refs aka. dynamic variables again
by Jenda (Abbot) on Apr 25, 2003 at 10:18 UTC

    > here, global vars win in speed, as you quoted.

    I think you meant "global vars LOSE in speed". Using global dynamic variables you did only 561.35 increments a second, while with the global hash you did 744.83 and with a lexical hash 794.74 increments per second.

    If you use timethese() with a negative first argument, the times per se are useless. You have to divide the number of iterations by the times to get anything meaningfull.

    Anyway you are right I should have included the global hash in the benchmark as well.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature