Update: I can't reproduce these results, so ignore this post. As
merlyn notes, a single search against an array is going to be more efficient by examining the array than building a hash. If you're going to be doing multiple searches, though, you're probably better off building a hash.
A quick Benchmark shows the spot on my system to be at 5 elements (comparing a linear scan versus building a hash):
1 0 wallclock secs (-0.89 usr + 0.00 sys = -0.89 CPU)
2 0 wallclock secs (-1.00 usr + 0.00 sys = -1.00 CPU)
3 -1 wallclock secs (-0.67 usr + 0.00 sys = -0.67 CPU)
4 1 wallclock secs (-0.35 usr + 0.01 sys = -0.34 CPU)
5 1 wallclock secs (-0.05 usr + 0.01 sys = -0.04 CPU)
6 0 wallclock secs ( 0.32 usr + 0.00 sys = 0.32 CPU)
7 2 wallclock secs ( 0.68 usr + 0.00 sys = 0.68 CPU)
8 2 wallclock secs ( 0.95 usr + 0.00 sys = 0.95 CPU)
9 1 wallclock secs ( 1.33 usr + 0.00 sys = 1.33 CPU)
10 3 wallclock secs ( 1.70 usr + 0.00 sys = 1.70 CPU)
So yah, you're better off going with a linear scan unless you're going to be working with more than a handful of elements.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.