vec( $vector, $bit, 1 ) ||= 1;
####
use strict;
use warnings;
use Benchmark qw[ cmpthese ];
use Inline C => 'DATA';
my( $vec1, $vec2 ) = ( ( chr(0) x 125000 ) x 2 );
cmpthese -3, {
inline => sub { mytest( $vec1, $_ ) for 0 .. 1e6-1; },
vec => sub { vec( $vec2, $_, 1 ) ||= 1 for 0 .. 1e6-1; },
};
warn "Different results" unless $vec1 eq $vec2;
__DATA__
__C__
int mytest(SV* sv_vec, unsigned int bit) {
STRLEN vecbytes; // Length of vector in bytes
unsigned char *myvec = (unsigned char *) SvPV(sv_vec, vecbytes);
if (bit/8 >= vecbytes) return 0; // Check in range
if (myvec[bit/8] & 1U<<(bit%8)) return 1; // Test if a bit is set
myvec[bit/8] |= 1U<<(bit%8); // Set bit (CHANGES $vector)
return 1;
}
##
##
C:\test>903727.pl
Rate inline vec
inline 3.11/s -- -60%
vec 7.77/s 150% --
C:\test>903727.pl
Rate inline vec
inline 3.10/s -- -60%
vec 7.77/s 151% --