I think you are benchmarking something else than you said you are interested in. You said you have an array of some size and want to empty it somehow and later assign as many values as there were originally. And you're benchmarking creating a brand new array, assigning it some initial value and then assigning the values. That looks like a very different thing. Plus 100 is a fairly small number.
The attached benchmark suggests that it's best to just empty the array by assigning an empty list and if the number of elements is big, resize the array afterwards.
use Benchmark qw(cmpthese); my $size = $ARGV[0] || 1000; my @array = (1..$size); sub undef_it { undef @array; $array[$_]=$_ foreach (0..$size-1); return; } sub empty_it { @array = (); $array[$_]=$_ foreach (0..$size-1); return; } sub empty_elements { undef $_ for @array; $array[$_]=$_ foreach (0..$size-1); return; } sub assign_list_of_undefs { @array = (undef) x $size; $array[$_]=$_ foreach (0..$size-1); return; } sub undef_resize { undef @array; $#array = $size; $array[$_]=$_ foreach (0..$size-1); return; } sub empty_resize { @array = (); $#array = $size; $array[$_]=$_ foreach (0..$size-1); return; } cmpthese( -10, { undef_it => \&undef_it, empty_it => \&empty_it, empty_elements => \&empty_elements, assign_list_of_undefs => \&assign_list_of_undefs, undef_resize => \&undef_resize, empty_resize => \&empty_resize, });
V:\Web\Distributor>c:\temp\benchEmpty.pl 100 Rate undef_it assign_list_of_undefs empty_ele +ments undef_resize empty_it empty_resize undef_it 41651/s -- -1% + -1% -4% -10% -10% assign_list_of_undefs 41936/s 1% -- + -0% -4% -10% -10% empty_elements 41948/s 1% 0% + -- -4% -10% -10% undef_resize 43501/s 4% 4% + 4% -- -6% -6% empty_it 46415/s 11% 11% + 11% 7% -- -0% empty_resize 46509/s 12% 11% + 11% 7% 0% -- V:\Web\Distributor>c:\temp\benchEmpty.pl 100 Rate undef_it empty_elements undef_resize ass +ign_list_of_undefs empty_resize empty_it undef_it 41006/s -- -3% -3% + -5% -9% -11% empty_elements 42097/s 3% -- -1% + -3% -7% -9% undef_resize 42406/s 3% 1% -- + -2% -6% -8% assign_list_of_undefs 43229/s 5% 3% 2% + -- -4% -7% empty_resize 45131/s 10% 7% 6% + 4% -- -2% empty_it 46280/s 13% 10% 9% + 7% 3% -- V:\Web\Distributor>c:\temp\benchEmpty.pl Rate assign_list_of_undefs empty_elements empt +y_resize undef_resize undef_it empty_it assign_list_of_undefs 4540/s -- -2% + -4% -6% -8% -8% empty_elements 4621/s 2% -- + -3% -5% -6% -7% empty_resize 4745/s 5% 3% + -- -2% -4% -4% undef_resize 4841/s 7% 5% + 2% -- -2% -2% undef_it 4919/s 8% 6% + 4% 2% -- -1% empty_it 4951/s 9% 7% + 4% 2% 1% -- V:\Web\Distributor>c:\temp\benchEmpty.pl 10000 Rate assign_list_of_undefs empty_elements undef +_it undef_resize empty_it empty_resize assign_list_of_undefs 447/s -- -2% +-6% -6% -10% -10% empty_elements 454/s 2% -- +-5% -5% -8% -8% undef_it 476/s 7% 5% + -- -0% -4% -4% undef_resize 477/s 7% 5% + 0% -- -4% -4% empty_it 494/s 11% 9% + 4% 4% -- -0% empty_resize 496/s 11% 9% + 4% 4% 0% -- V:\Web\Distributor>c:\temp\benchEmpty.pl 10000 Rate empty_elements assign_list_of_undefs undef +_it undef_resize empty_it empty_resize empty_elements 454/s -- -0% +-4% -6% -9% -10% assign_list_of_undefs 456/s 0% -- +-4% -6% -8% -9% undef_it 473/s 4% 4% + -- -2% -5% -6% undef_resize 483/s 6% 6% + 2% -- -3% -4% empty_it 498/s 10% 9% + 5% 3% -- -1% empty_resize 504/s 11% 10% + 6% 4% 1% --
BTW, the ()[0...100] doesn't seem to work the way you seem to expect it. It doesn't seem to generate a list of 101 undefs, rather it generates an empty list:
@a = ()[0..50]; print $#a;
In reply to Re: Emptying (and refilling) an array.
by Jenda
in thread Emptying (and refilling) an array.
by DStaal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |