DEV Community


Discussion on: Benchmarking or "How to count words really fast"

timclicks profile image
Tim McNamara

Really like that you have broken down all of the steps and taken the time to benchmark each of them.

Some other suggestions:

Maybe stream stdin, rather than reading the whole thing at the start? io::stdin().read_to_end(&mut buffer).ok(); feels expensive.

Perhaps pick a specialized hash function that's happy with short strings.

Maybe use Vec::with_capacity(words.len()) rather than the vec![] macro when you construct multiple and single. That'll avoid several allocations. Although it also will probably waste a lot of memory.

mrcryn profile image
Marc Ryan Riginding Author

Great ideas, i'll update my code and measure.

Im primarily interested in optimizing for runtime, but I think it would be interesting to see how the implementation differs if you optimize for space.