Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Replacing $mon with Jan, Feb, Mar by regex or other means

by BrowserUk (Patriarch)
on Apr 25, 2003 at 10:14 UTC ( [id://253124]=note: print w/replies, xml ) Need Help??


in reply to Replacing $mon with Jan, Feb, Mar by regex or other means

Put the 3-char abbrevs into a string and use substr to pick out the right one.

perl -e"for my $mon (0..11) { print substr('JanFebMarAprMayJunJulAugSe +pOctNovDec', $mon*3, 3), $/; }" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

Switch the quotes under unix.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Re: Replacing $mon with Jan, Feb, Mar by regex or other means
by RMGir (Prior) on Apr 25, 2003 at 11:48 UTC
    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.


    --
    Mike

    Edit: Added strftime variant which I had overlooked.

    Edit by tye, change PRE to CODE around long lines

      To make a better comparison, you should strftimeLookup do a getMonthNumber as well. Now it's the only case that isn't penalized by an extra subroutine call. Or you could eliminate the call from all cases, the lookup time won't be different depending on the number of the month. Just use a fixed month number (but don't hardcode it as a literal, or else the compiler might pre-calculate some calculations).

      Abigail

        I did consider that, but since the strftime variant was already losing badly, it didn't seem critical.

        You're right, though, wrapping the fetch of @fixedLocaltime in a sub (returning a ref to the array) call kills the performance of the strftime variant a bit more.

        $ perl testMonth.pl Benchmark: running inlineArrayLookup, preBuiltArrayLookup, strftimeLoo +kup, strftimeSubLookup, substrLookup, each for at least 2 CPU seconds +... inlineArrayLookup: 2 wallclock secs ( 2.12 usr + 0.00 sys = 2.12 CP +U) @ 513847.17/s (n=1089356) preBuiltArrayLookup: 2 wallclock secs ( 2.09 usr + 0.01 sys = 2.10 +CPU) @ 568700.00/s (n=1194270) strftimeLookup: 1 wallclock secs ( 1.81 usr + 0.35 sys = 2.16 CPU) +@ 223003.24/s (n=481687) strftimeSubLookup: 2 wallclock secs ( 1.74 usr + 0.26 sys = 2.00 CP +U) @ 182455.50/s (n=364911) substrLookup: 2 wallclock secs ( 2.19 usr + 0.01 sys = 2.20 CPU) @ +446835.91/s (n=983039) Rate strftimeSubLookup strftimeLookup substrLo +okup inlineArrayLookup preBuiltArrayLookup strftimeSubLookup 182455/s -- -18% +-59% -64% -68% strftimeLookup 223003/s 22% -- +-50% -57% -61% substrLookup 446836/s 145% 100% + -- -13% -21% inlineArrayLookup 513847/s 182% 130% + 15% -- -10% preBuiltArrayLookup 568700/s 212% 155% + 27% 11% --

        --
        Mike

        Edit by tye, change PRE to CODE around long lines

      I do use this, though I usually put the constant information into a constant.

      use constant MONTHS=> 'JanFebMarAprMayJunJulAugSepOctNovDec';

      You'll find that this has little if any effect on the performance.


      Examine what is said, not who speaks.
      1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
      2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
      3) Any sufficiently advanced technology is indistinguishable from magic.
      Arthur C. Clarke.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://253124]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-23 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found