Bit-Set
view release on metacpan or search on metacpan
use strict;
use warnings;
use Time::HiRes qw(gettimeofday tv_interval);
use Bit::Set qw(:all);
# Constants
use constant BPQW => 64; # bits per qword (8 bytes * 8 bits)
use constant BPB => 8; # bits per byte
# Test sizes and iterations
my @size_array = (
128, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 131072, 262144, 524288, 1048576
);
my $iterations = 1000;
# Benchmark function registry
my %benchmark_funcs = (
'Count' => {
code => \&benchmark_bit_count,
descr => 'Count the number of bits set in the bitset'
},
'Inter Count' => {
code => \&benchmark_bit_inter_count,
descr => 'Count the number of bits set in an intersection'
# Benchmark functions for each test type
sub benchmark_bit_count {
my ($size) = @_;
# Pre-create bitsets outside the benchmark
my $bit1 = Bit_new($size);
Bit_set( $bit1, int( $size / 2 ), $size - 1 );
Bit_bset( $bit1, 0 );
my $t0 = [gettimeofday];
my $result = Bit_count($bit1) for 1 .. $iterations;
my $t1 = [gettimeofday];
my $total_time = tv_interval $t0, $t1;
Bit_free( \$bit1 );
return $total_time;
}
sub benchmark_bit_inter_count {
my ($size) = @_;
# Pre-create bitsets outside the benchmark
my $bit1 = Bit_new($size);
my $bit2 = Bit_new($size);
Bit_set( $bit1, int( $size / 2 ), $size - 1 );
Bit_bset( $bit1, 0 );
my $t0 = [gettimeofday];
my $result = Bit_inter_count( $bit1, $bit2 ) for 1 .. $iterations;
my $t1 = [gettimeofday];
my $total_time = tv_interval $t0, $t1;
Bit_free( \$bit1 );
Bit_free( \$bit2 );
return $total_time;
}
sub run_benchmark {
my ( $test_name, $size, $benchmark_func ) = @_;
my $total_time = $benchmark_func->($size);
my $total_time_ns = $total_time * 1_000_000_000; # Convert to nanoseconds
# Calculate derived metrics
my $time_per_iteration = $total_time_ns / $iterations;
my $iterations_per_second = $iterations / $total_time;
# Format and print results
printf
"%-30s (size = %8d): %12.0f ns total\t%8.2f ns/iter\t%10.2e iter/s\n",
"Bit $test_name", $size, $total_time_ns, $time_per_iteration,
$iterations_per_second;
}
# Main benchmark execution
print "Running individual benchmarks...\n";
print "=" x 80, "\n";
for my $test_name ( sort keys %benchmark_funcs ) {
print "\nBenchmarking $test_name: $benchmark_funcs{$test_name}{descr}\n";
print "-" x 80, "\n";
for my $size (@size_array) {
lib/Bit/Set.pm view on Meta::CPAN
use strict;
use warnings;
use Time::HiRes qw(gettimeofday tv_interval);
use Bit::Set qw(:all);
# Constants
use constant BPQW => 64; # bits per qword (8 bytes * 8 bits)
use constant BPB => 8; # bits per byte
# Test sizes and iterations
my @size_array = (
128, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 131072, 262144, 524288, 1048576
);
my $iterations = 1000;
# Benchmark function registry
my %benchmark_funcs = (
'Count' => {
code => \&benchmark_bit_count,
descr => 'Count the number of bits set in the bitset'
},
'Inter Count' => {
code => \&benchmark_bit_inter_count,
descr => 'Count the number of bits set in an intersection'
lib/Bit/Set.pm view on Meta::CPAN
# Benchmark functions for each test type
sub benchmark_bit_count {
my ($size) = @_;
# Pre-create bitsets outside the benchmark
my $bit1 = Bit_new($size);
Bit_set( $bit1, int( $size / 2 ), $size - 1 );
Bit_bset( $bit1, 0 );
my $t0 = [gettimeofday];
my $result = Bit_count($bit1) for 1 .. $iterations;
my $t1 = [gettimeofday];
my $total_time = tv_interval $t0, $t1;
Bit_free( \$bit1 );
return $total_time;
}
sub benchmark_bit_inter_count {
my ($size) = @_;
# Pre-create bitsets outside the benchmark
my $bit1 = Bit_new($size);
my $bit2 = Bit_new($size);
Bit_set( $bit1, int( $size / 2 ), $size - 1 );
Bit_bset( $bit1, 0 );
my $t0 = [gettimeofday];
my $result = Bit_inter_count( $bit1, $bit2 ) for 1 .. $iterations;
my $t1 = [gettimeofday];
my $total_time = tv_interval $t0, $t1;
Bit_free( \$bit1 );
Bit_free( \$bit2 );
return $total_time;
}
sub run_benchmark {
my ( $test_name, $size, $benchmark_func ) = @_;
my $total_time = $benchmark_func->($size);
my $total_time_ns = $total_time * 1_000_000_000; # Convert to nanoseconds
# Calculate derived metrics
my $time_per_iteration = $total_time_ns / $iterations;
my $iterations_per_second = $iterations / $total_time;
# Format and print results
printf
"%-30s (size = %8d): %12.0f ns total\t%8.2f ns/iter\t%10.2e iter/s\n",
"Bit $test_name", $size, $total_time_ns, $time_per_iteration,
$iterations_per_second;
}
# Main benchmark execution
print "Running individual benchmarks...\n";
print "=" x 80, "\n";
for my $test_name ( sort keys %benchmark_funcs ) {
print "\nBenchmarking $test_name: $benchmark_funcs{$test_name}{descr}\n";
print "-" x 80, "\n";
for my $size (@size_array) {
( run in 0.488 second using v1.01-cache-2.11-cpan-71847e10f99 )