File: benchmark-stemmers.pl

package info (click to toggle)
liblingua-stem-perl 2.31-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,740 kB
  • sloc: perl: 1,836; makefile: 2
file content (188 lines) | stat: -rwxr-xr-x 6,166 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/perl

#########################
# Usage: ./benchmark-stemmers.pl voc.txt
#
# Runs speed (words/second) tests using randomly selected
# words from a word list.
#

use strict;
use warnings;

use lib qw(../lib);

use Lingua::Stem qw ();
use Lingua::Stem::En qw ();
use Time::HiRes qw(gettimeofday tv_interval);

my $snowball = eval { require Lingua::Stem::Snowball; };
if ($@) {
    $snowball = 0;
}

my $loops   = 100;
my $n_words = 3000;

my @word = grep chomp, <ARGV>;

#################################################
# Preload word list so we have identical runs
my @word_list = ();
my $s = $n_words;
print "Generating base word list...\n";
for (1..$s) {
  my $result;
  my $w = @word[rand(scalar(@word))];
  push (@word_list,$w);
}
print "Generating long word list...\n";
my $n = $n_words * $loops;
my @big_word_list = ();
foreach my $count (1..$loops) {
    push (@big_word_list, @word_list);
}
print "$n_words words repeated $loops times\n\n";
# Word by word, Lingua::Stem::Snowball
if ($snowball) {
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        foreach my $w (@word_list) {
            my ($result) = Lingua::Stem::Snowball::stem('en', $w);
        }
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem::Snowball, one word at a time, no caching: %8s words/second\n", int($n/$elapsed);
}

# Processed in batches, Lingua::Stem::Snowball
if ($snowball) {
    eval {
        my $start_time = [gettimeofday];
        for (my $i = 0; $i < $loops; $i++) { 
            my @results = Lingua::Stem::Snowball::stem('en',\@word_list);
            my $n_words_returned = @results;
            if ($n_words_returned <= 1) {
                use Data::Dumper; warn(Dumper(\@results));
                die(sprintf("Lingua::Stem::Snowball, %6s word batches, no caching:         failed\n", $n_words));
            }
        }
        my $elapsed = tv_interval($start_time);
        printf  "Lingua::Stem::Snowball, %6s word batches, no caching:%8s words/second\n", $n_words, int($n/$elapsed);
    };
    if ($@) {
        print "$@";
    }
 }

# Processed in one batch, Lingua::Stem::Snowball
if ($snowball) {
    eval {
        my $start_time = [gettimeofday];
        my @results = Lingua::Stem::Snowball::stem('en',\@big_word_list);
        my $n_words_returned = @results;
        if ($n_words_returned <= 1) {
            die(sprintf("Lingua::Stem::Snowball, one batch, no caching:                   failed\n", $n_words));
        }
        my $elapsed = tv_interval($start_time);
        printf  "Lingua::Stem::Snowball, one batch, no caching:          %8s words/second\n", int($n/$elapsed);
    };
    if ($@) {
        print "$@";
    }
}

# Word by word, Lingua::Stem
{
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        foreach my $w (@word_list) {
            my ($result) = Lingua::Stem::stem($w);
        }
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, one word at a time, no caching:           %8s words/second\n", int($n/$elapsed);
}


# Processed in batches, Lingua::Stem
{
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        my ($result) = Lingua::Stem::stem(@word_list);
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, %6s word batches, no caching:          %8s words/second\n", $n_words, int($n/$elapsed);
}

# Processed in one batch, Lingua::Stem
{
    my $start_time = [gettimeofday];
    my ($result) = Lingua::Stem::stem(@big_word_list);
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, one batch, no caching:                    %8s words/second\n", int($n/$elapsed);
}

# Word by word, Lingua::Stem, with caching
{
    Lingua::Stem::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        foreach my $w (@word_list) {
            my ($result) = Lingua::Stem::stem($w);
        }
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, one word at a time, cache level 2:        %8s words/second\n", int($n/$elapsed);
}

# Processed in batches with caching, Lingua::Stem
{
    Lingua::Stem::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        my ($result) = Lingua::Stem::stem(@word_list);
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, %6s word batches, cache level 2:       %8s words/second\n", $n_words, int($n/$elapsed);
}
# Processed in one batch with caching, Lingua::Stem
{
    Lingua::Stem::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    my ($result) = Lingua::Stem::stem(@big_word_list);
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem, one batch, cache level 2:                 %8s words/second\n", int($n/$elapsed);
}

# Word by word, Lingua::Stem::En, with caching
{
    Lingua::Stem::En::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        foreach my $w (@word_list) {
            my ($result) = Lingua::Stem::En::stem({ -words => [$w] });
        }
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem::En, one word at a time, cache level 2:        %8s words/second\n", int($n/$elapsed);
}

# Processed in batches with caching, Lingua::Stem:En
{
    Lingua::Stem::En::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    for (my $i = 0; $i < $loops; $i++) { 
        my ($result) = Lingua::Stem::En::stem({ -words => \@word_list });
    }
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem::En, %6s word batches, cache level 2:       %8s words/second\n", $n_words, int($n/$elapsed);
}
# Processed in one batch with caching, Lingua::Stem::En
{
    Lingua::Stem::En::stem_caching({ -level => 2});
    my $start_time = [gettimeofday];
    my ($result) = Lingua::Stem::En::stem({ -words => \@big_word_list });
    my $elapsed = tv_interval($start_time);
    printf  "Lingua::Stem::En, one batch, cache level 2:                 %8s words/second\n", int($n/$elapsed);
}