#!/usr/bin/perl use warnings; use strict; use Test::More; use Benchmark qw{ cmpthese }; my @input = split /\n/, << '__INPUT__'; 12-12 12-12-12 12-13-12-13 12-12-13-13 12-13-13-14 __INPUT__ sub lenno { my @back; my @i = @input; for (@i) { my %h; undef $h{$1} while s/(\d\d)//; push @back, join ("-", sort keys %h); } \@back } sub choro { my @back; my @i = @input; for (@i) { my %h; undef $h{$1} while m/(\d\d)/g; push @back, join ('-', sort keys %h); } \@back } is_deeply(lenno(), choro(), 'same'); done_testing(); cmpthese(-3, { lenno => 'lenno', choro => 'choro', });
Rate lenno choro lenno 25358/s -- -4% choro 26458/s 4% --
Perl 5.16.2, x86_64-linux-thread-multi.
Update: Significantly faster: use a hash slice undef @h{m/\d\d/g}; instead of the inner loop.
In reply to Re^2: How can I reduce this with a REGEX?
by choroba
in thread How can I reduce this with a REGEX?
by misterperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |