in reply to (tye)Re: japhy regex analysis: case study (RE question...)
in thread RE question...yup, another one ;)

Even on a different machine, I get these results (the machine has the 5.005 version of Benchmark.pm):
timethese( -3, { mod => sub { $x % 10 }, chop => sub { chop(my $x = $x) }, substr => sub { substr($x, -1) }, }); __END__ (they ran for at least 3 seconds) chop: 8707.14/s (n= 29256) substr: 43620.45/s (n=136532) mod: 48906.87/s (n=163838)
So on my machine, mod is much faster than chop; the unexplored substr approach is nearly as fast.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
(tye)Re2: japhy regex analysis: case study (RE question...)
by tye (Sage) on May 28, 2001 at 07:53 UTC

    The main difference probably isn't your computer vs. my computer. You left out the regex solutions which (the first time that the first one is called) "modify" the global $x by providing it with a string value.

    Since the chop solution makes a copy in order to avoid changing the value of the global $x, it also doesn't give the global $x a string value. So if the chop solution is run first, it has to stringify $x for every single call (as in your two runs but not in mine).

    Your substr solution also gives the global $x a string value, but it is getting run after the chop solution so it doesn't help (in my run, all of the regex solutions were run before the chop solution).

    So for another way to compare the regular expression versions to the mod version, you could force a stringification per call. I didn't come up with an eligant way to do this (and I came up with some pretty interesting but non-intuitive and mutually contradictory benchmark numbers so I'll just leave this to someone else).

            - tye (but my friends call me "Tye")
Re: Re: (tye)Re: japhy regex analysis: case study (RE question...)
by snafu (Chaplain) on May 30, 2001 at 01:56 UTC
    Geez! I could kick myself for not thinking of substr! >:\

    ----------
    - Jim