in reply to Print Number With Implied Decimal Point
Update: See the replies for more correct benchmarks and better solutions. Thanks, kennethk and Eliya.#!/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% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Print Number With Implied Decimal Point
by Eliya (Vicar) on Apr 18, 2012 at 15:22 UTC | |
by kennethk (Abbot) on Apr 18, 2012 at 15:38 UTC | |
by Eliya (Vicar) on Apr 18, 2012 at 15:46 UTC | |
by j355ga (Initiate) on Apr 19, 2012 at 14:01 UTC | |
|
Re^2: Print Number With Implied Decimal Point
by kennethk (Abbot) on Apr 18, 2012 at 15:08 UTC | |
by JavaFan (Canon) on Apr 18, 2012 at 17:11 UTC |