by lqdc13 · 11y ago · 0 comments
First, this can be done with one line in Python.
Second, it's almost a page of C++.
Third: if done in a 10 lines in python it is around 30% faster than the C++ version and 2x faster than the perl version.
Here is my reference Python version that also counts the words in the same non-code golf kind of way:
import sys
from collections import defaultdict
def wc(path):
d = defaultdict(int)
with open(path) as f:
for line in f:
for w in line.split():
d[w] += 1
return sum(d.values())
if __name__=='__main__':
path = sys.argv[1]
print(path, ':', wc(path))
EDIT: They are counting new lines correctly. for my $file ( @ARGV ) {
my $words;
open my $fh, '<', $file or die;
local $/;
say "$file: ", scalar(split /\s+/, <$fh>), " words";
}
EDIT: Changed an explicit count of the `split` list into an implicit count via `scalar`.* You need to loop over sys.argv[1:].
* You could use binary mode to avoid decoding (which the C++ avoids).
* You could use collections.Counter() and its `update` method, which should be both cleaner and faster on Python 3:
d = Counter()
with open(path) as f:
for line in f:
d.update(line.split()) +$path.IO.lines.wordsI'm wrong, though; the optimizations Counter applies only make it faster when each `update` takes an iterable of a large number of elements. This is unlikely to happen in our case.
You can actually bypass the Counter wrapper and use its accelerator directly:
from _collections import _count_elements
def wc(path):
d = {}
with open(path, "rb") as f:
for line in f:
_count_elements(d, line.split())
return sum(d.values())
However, this uses implementation details and is as such bad code.Nevertheless, defaultdict == win for counting things like if we actually wanted unique words in this case.
int word_count(const char *const filename)
{
std::ifstream file{filename};
return std::distance(std::istream_iterator<std::string>{file}, {});
}