3 ms·
The Perl script will be slower because there are some extra steps it's going through in that code. So it can be improved. First here is the base line (using m
by draegtun 13y ago
The Perl script will be slower because there are some extra steps it's going through in that code.
So it can be improved. First here is the base line (using my own transaction.list file):
$ time awk '{ SUM+=$5 } END {printf "%d\n", SUM}' transaction.list
7725580800
real 0m1.419s
user 0m1.391s
sys 0m0.027s
$ time perl -e 'my $sum = 0; while (<>) { my @F=split; $sum+=$F[4]} printf"%d\n", $sum' transaction.list
7725580800
real 0m3.453s
user 0m3.428s
sys 0m0.024s
So this can be improved upon by removing need to create that temporary @F array in each loop...
$ time perl -ne '$sum += (split)[4]; END {printf "%d\n", $sum}' transaction.list
7725580800
real 0m2.727s
user 0m2.700s
sys 0m0.026s
Better but has mentioned by dtamhk elsewhere here, split can be speeded up if you are able to give it a limit:
$ time perl -ne '$sum += (split /\s+/, $_, 5)[4]; END {printf "%d\n", $sum}' transaction.list
7725580800
real 0m1.657s
user 0m1.632s
sys 0m0.023s
So now pretty much identical to awk speed. And if you're able to use substr instead of split then you can make it significantly faster than awk:
$ time perl -ne '$sum += substr($_, 30, 12); END {printf "%d\n", $sum}' transaction.list
7725580800
real 0m0.368s
user 0m0.347s
sys 0m0.021s