I know you probably weren't serious.

But it turns out that your approach isn't as slow as I thought it would be. It's still slower than using an array, but not terribly.

Benchmark: running inlineArrayLookup, preBuiltArrayLookup, strftimeLoo +kup, substrLookup, each for at least 2 CPU seconds... inlineArrayLookup: 2 wallclock secs ( 2.12 usr + -0.01 sys = 2.11 CP +U) @ 543540.28/s (n=1146870) preBuiltArrayLookup: 1 wallclock secs ( 2.04 usr + -0.01 sys = 2.03 +CPU) @ 588310.34/s (n=1194270) strftimeLookup: 3 wallclock secs ( 1.76 usr + 0.37 sys = 2.13 CPU) +@ 216666.20/s (n=461499) substrLookup: 2 wallclock secs ( 2.09 usr + 0.00 sys = 2.09 CPU) @ +465374.16/s (n=972632) Rate strftimeLookup substrLookup inlineArrayLo +okup preBuiltArrayLookup strftimeLookup 216666/s -- -53% +-60% -63% substrLookup 465374/s 115% -- +-14% -21% inlineArrayLookup 543540/s 151% 17% + -- -8% preBuiltArrayLookup 588310/s 172% 26% + 8% --
Comparing the strftime version isn't totally fair, since it's not a simple "month->string" converter, but requires a localtime list. But as others have pointed out, it will do locales correctly.

Here's the benchmark code:
#!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); use POSIX 'strftime'; my @months=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); sub getMonthNumber { return int(rand(12)); } sub preBuiltArrayLookup { return $months[getMonthNumber()]; } sub inlineArrayLookup { return qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[getMont +hNumber()]; } sub substrLookup { return substr('JanFebMarAprMayJunJulAugSepOctNovDec',getMonthNumbe +r()*3,3); } # so as to not penalize strftime with localtime call my @fixedLocaltime=localtime; sub strftimeLookup { return strftime "%m",@fixedLocaltime; } cmpthese(-2, { 'preBuiltArrayLookup' => \&preBuiltArrayLookup, 'inlineArrayLookup' => \&inlineArrayLookup, 'substrLookup' => \&substrLookup, 'strftimeLookup' => \&strftimeLookup, } );

--
Mike

Edit: Added strftime variant which I had overlooked.

Edit by tye, change PRE to CODE around long lines


In reply to Re: Re: Replacing $mon with Jan, Feb, Mar by regex or other means by RMGir
in thread Replacing $mon with Jan, Feb, Mar by regex or other means by jonnyfolk

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.