3 ms·
I've seen Go code faster than Rust one, for example there was an interesting article years ago about counting words in a file: https://benhoyt.com/writings/co
by Thaxll 2y ago
I've seen Go code faster than Rust one, for example there was an interesting article years ago about counting words in a file:
https://benhoyt.com/writings/count-words/ https://benhoyt.com/writings/count-words/
- silisili 2y agoMaybe I'm still asleep, but can anyone who read this explain this part - map[string]*int instead of map[string]int so we only have to allocate once per unique word.
- jamamp 2y agoI believe this is because the code `counts[word]++` basically works by: 1. Hash word, retrieve the value from the counts map 2. Increment the value 3. Hash word, store the value into the counts map By change counts to a map of strings to pointers of ints, you only need to perform #3 once per unique word. Once you've read the int pointer, you need only write to the pointer and not re-hash the word and write that to the map, which can be expensive. https://github.com/benhoyt/countwords/blob/master/optimized.go#L66 https://github.com/benhoyt/countwords/blob/master/optimized....
- silisili 2y agoAh of course, the allocation is in the map setting work not the integer itself. That makes sense, thanks!
- kbolino 2y agoI am surprised this doesn't get optimized by the compiler. I assume this is necessary in the general case (map could be modified concurrently, causing the bucket for the key to move) but it obviously isn't here.
- kbolino 2y agoAnything nontrivial with a map in Rust is surprisingly tricky. I didn't know there were performance implications though.
- Yoric 2y agoYes, the default implementation of hashing in Rust is designed to be cryptographically-safe at the expense of speed. You can get a large speedup by replacing it, e.g. https://nnethercote.github.io/perf-book/hashing.html https://nnethercote.github.io/perf-book/hashing.html .