-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathx-perl-bench
executable file
·110 lines (90 loc) · 2.16 KB
/
x-perl-bench
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
#!/usr/bin/env perl
#
# Quick Perl benchmark
#
# TODO: write benchmark script as my_bench.pl on cwd
#
# Pedro Melo <[email protected]> November 2011
#
use strict;
use warnings;
use Benchmark 'cmpthese';
sub out;
sub abort;
my $filename;
$filename = shift(@ARGV) if @ARGV;
out "Type the Perl code for the versions you want to benchmark";
out "Benchmark script will be written as '$filename'" if $filename;
my @versions;
while (my $version = read_version()) {
my $script = 'sub {' . $version->{script} . '}';
my $sub = eval $script;
if ($@) {
out "Error evaluating your script: $@";
out "We will ignore this script, try again.";
next;
}
$version->{sub} = $sub;
push @versions, $version;
}
abort "We need at least one version to benchmark..." unless @versions;
if ($filename) {
open(my $fh, '>', $filename)
or abort "Could not open '$filename' for writting: $!";
print $fh
"#!perl\n\nuse strict;\nuse warnings;\nuse Benchmark 'cmpthese';\n\n";
my $i = 0;
foreach my $v (@versions) {
$i++;
print $fh "my \$version_$i = sub {\n$v->{script}};\n\n";
}
print $fh "\nprint \"Starting the benchmark...\\n\";\n\ncmpthese(0, {";
$i = 0;
foreach my $v (@versions) {
$i++;
print $fh " '$v->{name}' => \$version_$i,\n";
}
print $fh "});\n\n";
close($fh);
out "Script '$filename' generated";
}
else {
out "Starting the benchmark (this may take a while)...";
cmpthese(0, {map { ($_->{name} => $_->{sub}) } @versions});
}
###########
# Utilities
sub out {
return unless -t \*STDOUT;
print @_, "\n";
}
sub abort {
out @_;
exit(1);
}
sub read_version {
out
"Define the name for this version. Type . (dot) if no more versions are required.";
my $name = read_non_empty_line();
return if $name eq '.';
out "Write the code for '$name',\nend with a . (dot) on a single line.";
my $script = read_script();
return {name => $name, script => $script};
}
sub read_non_empty_line {
while (<>) {
chomp;
s/^\s+|\s+$//g;
return $_ if $_;
}
abort "Aborted...";
}
sub read_script {
my $script = '';
while (<>) {
chomp;
last if $_ eq '.';
$script .= $_ . "\n";
}
return $script;
}