Ancient

 view release on metacpan or  search on metacpan

bench/const.pl  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw(cmpthese);
use lib 'blib/lib', 'blib/arch';

use const qw/all/;
use Const::XS ();

print "=== const() scalar benchmark ===\n";
cmpthese(-2, {
    "const" => sub { const my $x => 42 },
    "Const::XS" => sub { Const::XS::const(my $y => 42) },
});
print "\n";

print "=== const() array benchmark ===\n";
cmpthese(-2, {
    "const" => sub { const my @arr => qw/a b c d e/ },
    "Const::XS" => sub { Const::XS::const(my @arr2 => qw/a b c d e/) },
});
print "\n";

print "=== const() hash benchmark ===\n";
cmpthese(-2, {
    "const" => sub { const my %h => (a => 1, b => 2, c => 3) },
    "Const::XS" => sub { Const::XS::const(my %h2 => (a => 1, b => 2, c => 3)) },
});
print "\n";

print "=== make_readonly scalar benchmark ===\n";
cmpthese(-2, {
    "const" => sub { my $x = 42; const::make_readonly($x) },
    "Const::XS" => sub { my $y = 42; Const::XS::make_readonly($y) },
});
print "\n";

print "=== make_readonly hashref benchmark ===\n";
cmpthese(-2, {
    "const" => sub { my $h = { a => 1, b => { c => 2 } }; const::make_readonly($h) },
    "Const::XS" => sub { my $h = { a => 1, b => { c => 2 } }; Const::XS::make_readonly($h) },
});
print "\n";

print "=== is_readonly benchmark ===\n";
const my $ro => 42;
Const::XS::const(my $ro2 => 42);
cmpthese(-2, {
    "const" => sub { const::is_readonly($ro) },
    "Const::XS" => sub { Const::XS::is_readonly($ro2) },
});
print "\n";

print "=== c() vs Const::XS::const for inline use ===\n";
cmpthese(-2, {
    "c()" => sub { my $x = const::c(42) },
    "Const::XS" => sub { Const::XS::const(my $y => 42) },
});
print "\n";

print "=" x 60, "\n";
print "=== DIFFERENT USES OF const ===\n";
print "=" x 60, "\n\n";

print "=== Accessing pre-created constants ===\n";



( run in 1.379 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )