Subject: | [PATCH] 2x faster LCS |
Hello,
I have optimized LCS function, so now it runs 96% faster when $keyGen is undef, the only change is removal of unnecessary calls of keygen functions. Looks like subroutine calls are really expensive in Perl.
Unfortunately my patch has one downside, when $keyGen is actually defined, LCS is 4% slower than it was without my changes. I don't think it's a problem, because typically LCS is used without $keyGen.
Patch is attached.
Cheers,
Tomasz
Test script:
use strict;
use warnings;
use Algorithm::Diff;
use Algorithm::OldDiff;
use Benchmark qw/:all/;
use Digest::MD5 qw/md5_hex/;
my @aa = (('z') x 100, 'a', 'b', 'c', ('dupa')x70000, 'd', 'kupa');
my @bb = (('z') x 100, 'a', 'b', 'c', ('dupz')x70000, 'f', 'kupa');
my $hash = sub { md5_hex(shift) };
cmpthese(50, {
'my patch' => sub { Algorithm::Diff::LCS(\@aa, \@bb) },
'orginal' => sub { Algorithm::OldDiff::LCS(\@aa, \@bb) },
});
cmpthese(50, {
'my patch' => sub { Algorithm::Diff::LCS(\@aa, \@bb, $hash) },
'original' => sub { Algorithm::OldDiff::LCS(\@aa, \@bb, $hash) },
});
Results:
PS E:\Dokumenty\difftest> perl .\lcs.pl
Rate orginal my patch
orginal 2.41/s -- -49%
my patch 4.72/s 96% --
Rate my patch original
my patch 1.39/s -- -3%
original 1.44/s 4% --
Subject: | faster_lcs.patch |
ÿþ- - - D i f f . p m . o l d 2 0 1 4 - 1 1 - 2 6 0 6 : 4 1 : 5 4 . 0 0 0 0 0 0 0 0 0 + 0 1 0 0
+ + + D i f f . p m 2 0 1 4 - 1 2 - 2 3 2 1 : 0 3 : 5 1 . 0 0 0 0 0 0 0 0 0 + 0 1 0 0
@ @ - 4 2 , 7 + 4 2 , 7 @ @
f o r (