I have a file which contains several thousand numbers, each on it's own line:
34
42
11
6
2
99
...
I'm looking to write a script which will print the sum of all numbers in the file. I've got a solution, but it's not very efficient. (It takes several minutes to run.) I'm looking for a more efficient solution. Any suggestions?
This is straight Bash:
Perl 6
I don't know if you can get a lot better than this, considering you need to read through the whole file.
With Ruby:
Here is a solution using python with a generator expression. Tested with a million numbers on my old cruddy laptop.
Just for fun, let's benchmark it:
I aborted the sed run after 5 minutes
I've been diving to lua, and it is speedy:
and while I'm updating this, ruby:
Heed Ed Morton's advice: using
$1
vs using
$0