blib/lib/Text/Prefix/trie.pl untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 126 SCAR 6.9
CC
1
Cov
0%
reset_counters
2.0
print_counters
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Digest::SHA1 qw(sha1_hex);
5 use Data::Dumper;
6 use Time::HiRes qw(time);
7 use blib;
8 use Text::Prefix::XS;
9 use Log::Fu;
10  
11 my %index;
12 my %fullmatch;
13 my $matches = 0;
14 my $match_first_pass = 0;
15 my $not_filtered = 0;
16  
17 sub reset_counters {
18 $matches = 0;
19 $match_first_pass = 0;
20 $not_filtered = 0;
21 }
22  
23 sub print_counters {
24 printf("Got %d matches\n", $matches);
25 printf("Got %d matches on first pass\n",
26 $match_first_pass);
27 printf("Got %d non-filtered\n", $not_filtered);
28 }
29  
30 #Build string list, so that we can accurately benchark the searching
31 my $STRING_COUNT = 2_000_000;
32 my $TERM_COUNT = 20;
33 my $PREFIX_MIN = 5;
34 my $PREFIX_MAX = 15;
35  
36 my @strings = map substr(sha1_hex($_),0, 30), (0..$STRING_COUNT);
37  
38 my @terms;
39 while(@terms < $TERM_COUNT) {
40 my $str = $strings[int(rand($STRING_COUNT))];
41 my $prefix = substr($str, 0,
42 int(rand($PREFIX_MAX - $PREFIX_MIN)) + $PREFIX_MIN);
43 push @terms, $prefix;
44 }
45  
46 #Build index;
47 my $MIN_INDEX = 100;
48 foreach my $term (@terms) {
49 if(length($term) < $MIN_INDEX) {
50 $MIN_INDEX = length($term);
51 }
52  
53 my @chars = split(//, $term);
54 while(@chars) {
55 $index{join("", @chars)} = 1;
56 pop @chars;
57 }
58 $fullmatch{$term} = 1;
59  
60 }
61 @terms = sort { length $b <=> length $a || $a cmp $b } @terms;
62  
63 printf("%s\n", join(', ', keys %fullmatch));
64  
65 my $begin_time = time();
66 print "Beginning search\n";
67 CHECK_TERM:
68 foreach my $str (@strings) {
69 my $j = 1;
70 while($j <= $MIN_INDEX) {
71 if(!exists $index{substr($str,0,$j)}){
72 next CHECK_TERM;
73 }
74 $j++;
75 }
76 $not_filtered++;
77 #The prefix matches
78 foreach my $term (@terms) {
79 if(substr($str,0,length($term)) eq $term) {
80 $matches++;
81 next CHECK_TERM;
82 }
83 }
84 };
85  
86 my $now = time();
87 my $duration = $now-$begin_time;
88 printf("my Trie: Duration: %0.2f\n", $duration);
89 print_counters();
90  
91 #Try large regex version..
92 reset_counters();
93 my $BIG_RE = join '|',
94 map quotemeta,
95 sort { length $b <=> length $a || $a cmp $b }
96 @terms;
97 $BIG_RE = qr/^($BIG_RE)/;
98  
99  
100 log_infof("Have %d terms", scalar @terms);
101  
102 print $BIG_RE . "\n";
103 $begin_time = time();
104 foreach my $str (@strings) {
105 if($str =~ $BIG_RE) {
106 $matches++;
107 }
108 }
109 $duration = time() - $begin_time;
110 printf("BIG RE: Duration: %0.2f\n", $duration);
111 print_counters();
112  
113 reset_counters();
114 my $xs_search = prefix_search_build(\@terms);
115 $begin_time = time();
116  
117 foreach my $str (@strings) {
118 if(my $result = prefix_search($xs_search, $str)) {
119 $matches++;
120 }
121 }
122 $duration = time() - $begin_time;
123 printf("C Implementation: %0.2f\n", $duration);
124 print_counters();
125 Text::Prefix::XS::print_optimized("");