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


In reply to Re: Hashes/Scalars and Memory Usage by InfiniteSilence
in thread Hashes/Scalars and Memory Usage by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.