use strict; use Benchmark qw(cmpthese); use constant NUM_OF_VARS => 10000; use constant NUM_OF_TESTS => 1000; my @names = (); # Meaningless, getting rid of warning *NULL = *NULL; open(NULL, '>/dev/null') or die "Couldn't open /dev/null: $!; stopped"; ## Initialize a bunch of variables to interpolate { no strict 'refs'; my $var_name = 'aa'; for ( 0 .. NUM_OF_VARS ) { $ {"Q::$var_name" } = "Name is '$var_name', number $_"; push(@names, "\$Q::$var_name"); $var_name++; } } # Build up two subroutine strings to eval my $list_print = "print NULL "; $list_print .= join(",\n", map( qq{'$_ is ', $_, "\\n"}, @names) ); $list_print .= ';'; my $build_print = "my \$string = '';\n"; foreach my $name (@names) { $build_print .= qq{\$string .= '$name is ';\n}; $build_print .= qq{\$string .= $name;\n}; $build_print .= qq{\$string .= "\\n";\n}; } $build_print .= "print NULL \$string;\n"; # Benchmark the subs cmpthese(NUM_OF_TESTS, { 'build_print' => $build_print, 'list_print' => $list_print, });