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
|
=head1 DESCRIPTION
The B<assert> keyword is ignored at runtime when STRICT mode is disabled,
so it should not impact the performance of the code.
This benchmark demonstrates that the performance of code using the B<assert> keyword is nearly identical to code without it.
=head1 SYNOPSIS
% perl bench/compare-no-assertion.pl
=head1 RESULT
Rate no assertion with assertion
no assertion 14492754/s -- -1%
with assertion 14705882/s 1% --
=cut
use v5.40;
use Benchmark qw(cmpthese);
BEGIN {
$ENV{PERL_ASSERT_ENABLED} = 0;
}
use Syntax::Keyword::Assert;
# Function with assertion block but it is ignored at runtime
sub with_assertion($message) {
assert( defined $message );
return $message;
}
# Function without assertion block
sub no_assertion($message) {
return $message;
}
cmpthese(10000000, {
'with assertion' => sub { with_assertion('world') },
'no assertion' => sub { no_assertion('world') },
});
|