in reply to Print Number With Implied Decimal Point

I am not sure whether this answers your question or whether the benchmark is correct, but I hope it can help you:
#!/usr/bin/perl use warnings; use strict; use feature 'say'; use bigint; use Benchmark qw/cmpthese/; my @chars = ('0' .. '9', 'a' .. 'f'); my @list = map {join q(), map $chars[rand @chars], 1 .. 32} 1 .. 100; say for @list; $_ = hex $_ for @list; say for @list; cmpthese(0, { substr => sub { my @l = @list; substr $_, -2, 0, '.' for @l; }, regex => sub { my @l = @list; s/(..)$/.$1/ for @l; } }); __END__ Rate substr regex substr 327/s -- -14% regex 380/s 16% --
Update: See the replies for more correct benchmarks and better solutions. Thanks, kennethk and Eliya.

Replies are listed 'Best First'.
Re^2: Print Number With Implied Decimal Point
by Eliya (Vicar) on Apr 18, 2012 at 15:22 UTC

    use bigint applied globally severely distorts the results.

    As both of your test cases work on strings anyway, you'd get a more useful comparison when restricting the scope of bigint to where it's required.

    With your original version, I get on my machine:

    Rate substr regex substr 481/s -- -14% regex 558/s 16% --

    while with the restricted scope of bigint, I get

    #!/usr/bin/perl use warnings; use strict; use feature 'say'; use Benchmark qw/cmpthese/; my @list; { use bigint; my @chars = ('0' .. '9', 'a' .. 'f'); @list = map {join q(), map $chars[rand @chars], 1 .. 32} 1 .. 100; say for @list; $_ = (hex $_)."" for @list; say for @list; } cmpthese(0, { substr => sub { my @l = @list; substr $_, -2, 0, '.' for @l; }, regex => sub { my @l = @list; s/(..)$/.$1/ for @l; } }); __END__ Rate regex substr regex 5672/s -- -80% substr 28469/s 402% --
        the timing difference between it and substr is pretty small

        Yes, sure.  My main point was the side effects of applying use bigint globally (which completely washes out any difference there might be), and not so much to support that substr is faster than regex.

      Limiting the bigint scope makes a HUGE difference in my tests. 2,100 rows per second vs 700 per sec. Thanks for the tip!
Re^2: Print Number With Implied Decimal Point
by kennethk (Abbot) on Apr 18, 2012 at 15:08 UTC

    If you want to make the regex solution run much more quickly, use Look Around Assertions in place of the capture and reinsert.

    cmpthese(0, { substr => sub { my @l = @list; substr $_, -2, 0, '.' for @l; }, regex => sub { my @l = @list; s/(..)$/.$1/ for @l; }, regex2 => sub { my @l = @list; s/(?=..)$/./ for @l; }, });
    yields
    Rate substr regex regex2 substr 73.1/s -- -39% -71% regex 121/s 65% -- -52% regex2 250/s 242% 107% --

    Update: As per JavaFan's comment, I had a typo in my sub. Replaced s/(?=..)$/./ with s/(?=..$)/./ which returns the correct result, but at substantially poorer performance.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      These numbers look too good to be true:
      $_ = "9876543"; s/(?=..)$/./; say; __END__ 9876543
      You cannot actual replace a zero-width assertion.

      And before benchmarking, you should always check whether you're producing the correct results. Bogus solutions noone cares for, even if they're fast.