From: Vincent Pit Date: Sat, 16 May 2009 12:03:19 +0000 (+0200) Subject: Speed up vfill_pp() X-Git-Tag: v0.06~12 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=01ef742d705c0bd6577139d1ee8a2808101336da;p=perl%2Fmodules%2FScalar-Vec-Util.git Speed up vfill_pp() --- diff --git a/lib/Scalar/Vec/Util.pm b/lib/Scalar/Vec/Util.pm index c80d642..12d049c 100644 --- a/lib/Scalar/Vec/Util.pm +++ b/lib/Scalar/Vec/Util.pm @@ -81,8 +81,17 @@ sub vfill_pp { (undef, my $s, my $l, my $x) = @_; croak "Invalid argument" unless _alldef @_; return unless $l; - $x = 1 if $x; - vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; + $x = ~0 if $x; + my $SIZE = 32; + my $t = int($s / $SIZE) + 1; + my $u = int(($s + $l) / $SIZE); + if ($SIZE * $t < $s + $l and $t <= $u) { + vec($_[0], $_, 1) = $x for $s .. $SIZE * $t - 1; + vec($_[0], $_, $SIZE) = $x for $t .. $u - 1; + vec($_[0], $_, 1) = $x for $SIZE * $u .. $s + $l - 1; + } else { + vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; + } } =head2 C<< vcopy $from => $from_start, $to => $to_start, $length >> diff --git a/t/10-veq-pp.t b/t/10-veq-pp.t index ffd721a..98fea99 100644 --- a/t/10-veq-pp.t +++ b/t/10-veq-pp.t @@ -5,7 +5,7 @@ use warnings; use Test::More 'no_plan'; -use Scalar::Vec::Util qw/SVU_SIZE/; +use Scalar::Vec::Util; eval { Scalar::Vec::Util::veq_pp(undef, 0, my $y, 0, 0) }; like($@, qr/Invalid\s+argument/, 'first argument undef croaks'); @@ -18,12 +18,15 @@ like($@, qr/Invalid\s+argument/, 'fourth argument undef croaks'); eval { Scalar::Vec::Util::veq_pp(my $x, 0, my $y, 0, undef) }; like($@, qr/Invalid\s+argument/, 'fifth argument undef croaks'); -my $p = SVU_SIZE; -$p = 8 if $p < 8; +my $p = 8; my $n = 3 * $p; my $q = 1; -*myfill = *Scalar::Vec::Util::vfill_pp; +sub myfill { + (undef, my $s, my $l, my $x) = @_; + $x = 1 if $x; + vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; +} sub rst { myfill($_[0], 0, $n, 0) } diff --git a/t/11-veq.t b/t/11-veq.t index 418e13e..3d141b3 100644 --- a/t/11-veq.t +++ b/t/11-veq.t @@ -23,7 +23,11 @@ $p = 8 if $p < 8; my $n = 3 * $p; my $q = 1; -*myfill = *Scalar::Vec::Util::vfill_pp; +sub myfill { + (undef, my $s, my $l, my $x) = @_; + $x = 1 if $x; + vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; +} sub rst { myfill($_[0], 0, $n, 0) } diff --git a/t/20-vfill-pp.t b/t/20-vfill-pp.t index 5986db8..aaf3618 100644 --- a/t/20-vfill-pp.t +++ b/t/20-vfill-pp.t @@ -15,3 +15,34 @@ eval { Scalar::Vec::Util::vfill_pp(my $x, 0, undef, 0) }; like($@, qr/Invalid\s+argument/, 'third argument undef croaks'); eval { Scalar::Vec::Util::vfill_pp(my $x, 0, 0, undef) }; like($@, qr/Invalid\s+argument/, 'fourth argument undef croaks'); + +my $p = 8; +my $n = 3 * $p; +my $q = 1; + +sub myfill { + (undef, my $s, my $l, my $x) = @_; + $x = 1 if $x; + vec($_[0], $_, 1) = $x for $s .. $s + $l - 1; +} + +*myeq = *Scalar::Vec::Util::veq_pp; + +sub rst { myfill($_[0], 0, $n, 0); $_[0] = '' } + +my ($v, $c) = ('') x 2; + +my @s = ($p - $q) .. ($p + $q); +for my $s (@s) { + for my $l (0 .. $n - 1) { + next if $s + $l > $n; + rst $c; + myfill($c, 0, $s, 0); + myfill($c, $s, $l, 1); + rst $v; + Scalar::Vec::Util::vfill_pp($v, 0, $s, 0); + Scalar::Vec::Util::vfill_pp($v, $s, $l, 1); + ok(myeq($v, 0, $c, 0, $n), "vfill_pp $s, $l"); + is(length $v, length $c, "vfill_pp $s, $l length"); + } +}