You are actually concerned about speed but it's faster to run a short script without loading List::Util if the arrays being summed are tiny
Hm.
Hard to see where though. Most phones have 4GB and even the original Raspberry Pi had 512MB.
#! perl -sl use List::Util qw[ sum ]; our $N //= 10; print sum( 1 .. $N );
And WithoutListUtil.pl:
#! perl -sl #use List::Util qw[ sum ]; our $N //= 10; my $t = 0; $t += $_ for 1 .. $N; print $t;
Darned if I can find any significant difference for as few as 3 numbers to add. Pretty consistently 4-5/100ths for both to start perl, count the numbers and return to the prompt:
[16:27:32.60] C:\test>for /l %i in (1,1,10) do WithListUtil.pl -N=3 [16:27:55.68] C:\test>WithListUtil.pl -N=3 [16:27:55.73] C:\test>WithListUtil.pl -N=3 [16:27:55.79] C:\test>WithListUtil.pl -N=3 [16:27:55.85] C:\test>WithListUtil.pl -N=3 [16:27:55.90] C:\test>WithListUtil.pl -N=3 [16:27:55.95] C:\test>WithListUtil.pl -N=3 [16:27:56.00] C:\test>WithListUtil.pl -N=3 [16:27:56.05] C:\test>WithListUtil.pl -N=3 [16:27:56.10] C:\test>WithListUtil.pl -N=3 [16:27:56.15] C:\test>WithListUtil.pl -N=3 [16:27:56.20] C:\test>for /l %i in (1,1,10) do WithoutListUtil.pl -N=3 [16:28:22.44] C:\test>WithoutListUtil.pl -N=3 [16:28:22.49] C:\test>WithoutListUtil.pl -N=3 [16:28:22.54] C:\test>WithoutListUtil.pl -N=3 [16:28:22.59] C:\test>WithoutListUtil.pl -N=3 [16:28:22.63] C:\test>WithoutListUtil.pl -N=3 [16:28:22.67] C:\test>WithoutListUtil.pl -N=3 [16:28:22.71] C:\test>WithoutListUtil.pl -N=3 [16:28:22.76] C:\test>WithoutListUtil.pl -N=3 [16:28:22.80] C:\test>WithoutListUtil.pl -N=3 [16:28:22.84] C:\test>WithoutListUtil.pl -N=3 [16:28:22.88] C:\test>
It's hard to see the loading time of List::Util being a significant factor in any real code.
I thought it came with the core for most distributions. I 'spose someone somewhere might be running a bastardized, cut-down version.
In reply to Re^4: Best way to sum an array?
by BrowserUk
in thread Best way to sum an array?
by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |