Legba

 view release on metacpan or  search on metacpan

bench/benchmark.pl  view on Meta::CPAN


use Legba qw/bench_slot/;

# Warm up
bench_slot("warmup");
my $x = bench_slot();

print "=== Legba Slot Benchmark ===\n\n";

# Test 1: Getter performance
print "--- Getter Performance (10M iterations) ---\n";
my $result = timethese(10_000_000, {
    'slot_getter' => sub { my $v = bench_slot(); },
});
print "\n";

# Test 2: Setter performance  
print "--- Setter Performance (10M iterations) ---\n";
$result = timethese(10_000_000, {
    'slot_setter' => sub { bench_slot(42); },
});
print "\n";

# Test 3: Mixed get/set
print "--- Mixed Get/Set (5M iterations each) ---\n";
$result = timethese(5_000_000, {
    'get_then_set' => sub { 
        my $v = bench_slot();
        bench_slot($v + 1);
    },
});
print "\n";

# Test 4: Compare to hash access
print "--- Comparison: Slot vs Hash vs Scalar (10M iterations) ---\n";
my %hash = (bench => 0);
my $scalar = 0;
bench_slot(0);

cmpthese(10_000_000, {
    'legba_get' => sub { my $v = bench_slot(); },
    'hash_get'  => sub { my $v = $hash{bench}; },
    'scalar_get'=> sub { my $v = $scalar; },
});
print "\n";



( run in 2.453 seconds using v1.01-cache-2.11-cpan-71847e10f99 )