#!/usr/bin/perl use Benchmark qw(cmpthese); use Inline C; my $s1 = '/home/usernames/doejohnwilson'; my $s2 = '/home/usernames/doejanemary'; cmpthese(-1, { 'standard' => sub { my $pos = 0; $pos++ while substr($s1, $pos, 1) eq substr($s2, $pos, 1); my $result = substr($s1, 0, $pos); # die unless $result eq "/home/usernames/doej"; }, 'fancy-XOR' => sub { my $common = $s1 ^ $s2; my $common_length = 0; if ($common =~ /^(\x00+)/) { $common_length = length $1; }; my $result = substr($s1, 0, $common_length); # die unless $result eq "/home/usernames/doej"; }, 'standard-XS' => sub { my $common_length = left_anchored_common_substr_len($s1, $s2); my $result = substr($s1, 0, $common_length); # die unless $result eq "/home/usernames/doej"; } }); __END__ __C__ int left_anchored_common_substr_len(char* s1, char* s2) { int c = 0; while (*s1++ == *s2++) { c++; } return c; } #### Rate standard fancy-XOR standard-XS standard 109227/s -- -77% -92% fancy-XOR 468114/s 329% -- -64% standard-XS 1291788/s 1083% 176% --