./bench.pl
+=head1 OPTIONS
+
+=over 4
+
+=item B<-c>, B<--cflags>=I<flags>
+
+Flags to pass to the C or C++ compiler.
+
+=item B<-i>, B<--iterations>=I<integer>
+
+Say how many times a single test of the bench must be run. If
+negative, specify the minimum number of CPU seconds to run. Defaults
+to -3.
+
+=item B<-v>, B<--verbose>
+
+Raise the verbosity level. Currently only affects B<--help>.
+
+=back
+
=cut
use IO::File;
-use Benchmark qw (:all);
+
+##################################################################
+
+=head1 VARIABLES
+
+=over 4
+
+=item C<$bison>
+
+The Bison program to use to compile the grammar.
+
+=item C<$cc>
+
+The C compiler.
+
+=item C<$cxx>
+
+The C++ compiler.
+
+=item C<$cflags>
+
+Compiler flags (C or C++).
+
+=item C<$iterations>
+
+The number of times the parser is run for a bench.
+
+=item C<$verbose>
+
+Verbosity level.
+
+=back
+
+=cut
my $bison = $ENV{'BISON'} || '@abs_top_builddir@/tests/bison';
my $cc = $ENV{'CC'} || 'gcc';
my $cxx = $ENV{'CXX'} || 'g++';
+my $cflags = '';
+my $iterations = -3;
+my $verbose = 0;
-##################################################################
-
-=head2 Functions
+=head1 FUNCTIONS
=over 4
+=item C<verbose($level, $message)>
+
+Report the C<$message> is C<$level> E<lt>= C<$verbose>.
+
+=cut
+
+sub verbose($$)
+{
+ my ($level, $message) = @_;
+ print STDERR $message
+ if $level <= $verbose;
+}
+
=item C<directives($bench, @directive)>
Format the list of directives for Bison for bench named C<$bench>.
sub directives($@)
{
- my ($bench, @directives) = @_;
+ my ($bench, @directive) = @_;
my $res = "/* Directives for bench `$bench'. */\n";
- for my $d (@directives)
+ for my $d (@directive)
{
$res .= $d . "\n"
unless $d eq '%variant';
return $res;
}
-=item C<triangular_grammar ($base, $max, @directives)>
+=item C<triangular_grammar ($base, $max, @directive)>
Create a large triangular grammar which looks like :
| "1" "2" "3" "4" "5" END { $$ = 5; }
;
-C<$base> is the base name for the file to create (C<$base.y>).
+C<$base> is the base name for the file to create (F<$base.y>).
C<$max> is the number of such rules (here, 5). You may pass
-additional Bison C<@directives>.
+additional Bison C<@directive>.
The created parser is self contained: it includes its scanner, and
source of input.
sub triangular_grammar ($$$)
{
- my ($base, $max, @directives) = @_;
- my $directives = directives ($base, @directives);
+ my ($base, $max, @directive) = @_;
+ my $directives = directives ($base, @directive);
my $out = new IO::File ">$base.y"
or die;
=item C<calc_input ($base, $max)>
-Generate the input file C<$base.input> for the calc parser. The input
+Generate the input file F<$base.input> for the calc parser. The input
is composed of two expressions. The first one is using left recursion
only and consumes no stack. The second one requires a deep stack.
These two expressions are repeated C<$max> times in the output file.
}
##################################################################
-=item C<calc_grammar ($base, $max, @directives)>
+=item C<calc_grammar ($base, $max, @directive)>
-Generate a Bison file C<$base.y> for a calculator parser in C. Pass
-the additional Bison C<@directives>. C<$max> is ignored, but left to
+Generate a Bison file F<$base.y> for a calculator parser in C. Pass
+the additional Bison C<@directive>. C<$max> is ignored, but left to
have the same interface as C<triangular_grammar>.
=cut
sub calc_grammar ($$$)
{
- my ($base, $max, @directives) = @_;
- my $directives = directives ($base, @directives);
+ my ($base, $max, @directive) = @_;
+ my $directives = directives ($base, @directive);
my $out = new IO::File ">$base.y"
or die;
##################################################################
-=item C<variant_grammar ($base, $max, @directives)>
+=item C<variant_grammar ($base, $max, @directive)>
-Generate a Bison file C<$base.y> that uses, or not, the Boost.Variants
-depending on the C<@directives>.
+Generate a Bison file F<$base.y> that uses, or not, the Boost.Variants
+depending on the C<@directive>.
=cut
sub variant_grammar ($$$)
{
- my ($base, $max, @directives) = @_;
- my $directives = directives ($base, @directives);
- my $variant = grep { '%variant' } @directives;
+ my ($base, $max, @directive) = @_;
+ my $directives = directives ($base, @directive);
+ my $variant = grep { $_ eq '%variant' } @directive;
my $out = new IO::File ">$base.y"
or die;
print $out <<EOF;
-%debug
%language "C++"
%defines
+$directives
-%code requires // code for the .hh file
+%code requires // variant.h
{
#include <string>
}
-%code // code for the .cc file
+%code // variant.c
{
#include <algorithm>
#include <iostream>
// Prototype of the yylex function providing subsequent tokens.
static yy::parser::token_type yylex(yy::parser::semantic_type* yylval);
-#define STAGE_MAX $max
+#define STAGE_MAX ($max * 10)
#define USE_VARIANTS $variant
#if USE_VARIANTS
# define IF_VARIANTS(True, False) True
int main(int argc, char *argv[])
{
yy::parser p;
+#if YYDEBUG
p.set_debug_level(!!getenv("YYDEBUG"));
+#endif
p.parse();
return 0;
}
system ("$bison $base.y -o $base.c") == 0
or die;
- system ("$compiler -o $base -O3 -I /opt/local/include $base.c") == 0
+ system ("$compiler -o $base $cflags $base.c") == 0
or die;
}
Generate benches for C<$gram>. C<$gram> should be C<calc> or
C<triangle>. C<%bench> is a hash of the form:
- C<$name> => C<@directives>
+ $name => @directive
-where C<$name> is the name of the bench, and C<@directives> are the
+where C<$name> is the name of the bench, and C<@directive> are the
Bison directive to use for this bench. All the benches are compared
against each other, repeated 50 times.
{
my ($gram, %test) = @_;
+ use Benchmark qw (:all :hireswallclock);
+
# Set up the benches as expected by timethese.
my %bench;
+ # For each bench, capture the size.
+ my %size;
while (my ($name, $directives) = each %test)
{
- print STDERR "$name\n";
+ verbose 1, "Generating $name\n";
# Call the Bison input file generator.
my $generator = "$gram" . "_grammar";
&$generator ($name, 200, @$directives);
+ # Compile the executable.
compile ($name);
$bench{$name} = "system ('./$name');";
+ chop($size{$name} = `wc -c <$name`);
}
- print "$gram:\n";
# Run the benches.
- my $res = timethese (50, \%bench, 'nop');
- # Output the result.
+ #
+ # STYLE can be any of 'all', 'none', 'noc', 'nop' or 'auto'. 'all'
+ # shows each of the 5 times available ('wallclock' time, user time,
+ # system time, user time of children, and system time of
+ # children). 'noc' shows all except the two children times. 'nop'
+ # shows only wallclock and the two children times. 'auto' (the
+ # default) will act as 'all' unless the children times are both
+ # zero, in which case it acts as 'noc'. 'none' prevents output.
+ verbose 1, "Running the benches for $gram\n";
+ my $res = timethese ($iterations, \%bench, 'nop');
+
+ # Output the speed result.
cmpthese ($res, 'nop');
+
+ # Display the sizes.
+ print "Sizes:\n";
+ my $width = 10;
+ for my $bench (keys %size)
+ {
+ $width = length $bench
+ if $width < length $bench;
+ }
+ for my $bench (keys %size)
+ {
+ printf "%${width}s: %5dkB\n", $bench, int ($size{$bench} / 1024);
+ }
}
bench_grammar
('variant',
(
- "union" => [],
- "variant" => ['%variant'],
+ "union" => [],
+ "variant" => ['%variant'],
+ "union-debug" => ['%debug'],
+ "variant-debug" => ['%debug', '%variant'],
+ )
+ );
+}
+
+=item C<bench_fusion_parser ()>
+
+Bench the C++ lalr1.cc parser using Boost.Variants or %union.
+
+=cut
+
+sub bench_fusion_parser ()
+{
+ bench_grammar
+ ('variant',
+ (
+ "split" => [],
+ "fused" => ['%skeleton "lalr1-fusion.cc"'],
)
);
}
############################################################################
-print STDERR "Using bison=$bison, cc=$cc, cxx=$cxx.\n";
+sub help ($)
+{
+ my ($verbose) = @_;
+ use Pod::Usage;
+ # See <URL:http://perldoc.perl.org/pod2man.html#NOTES>.
+ pod2usage( { -message => "Bench Bison parsers",
+ -exitval => 0,
+ -verbose => $verbose,
+ -output => \*STDOUT });
+}
+
+sub getopt ()
+{
+ use Getopt::Long;
+ %option = ("h|help" => sub { help ($verbose) },
+ "v|verbose" => sub { ++$verbose },
+ "c|cflags=s" => \$cflags,
+ "i|iterations=i" => \$iterations);
+ Getopt::Long::Configure ("bundling", "pass_through");
+ GetOptions (%option)
+ or exit 1;
+}
+
+######################################################################
+
+getopt;
+verbose 1, "Using bison=$bison.\n";
+verbose 1, "Using cc=$cc.\n";
+verbose 1, "Using cxx=$cxx.\n";
+verbose 1, "Using cflags=$cflags.\n";
# bench_push_parser();
-bench_variant_parser();
+# bench_variant_parser();
+bench_fusion_parser();
### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables: