- Read file in one thread pool, streaming the chunks to...
- ...another thread pool, tokenise, count, sort the chunks and send them to ...
- ... merge in another thread pool. (basically map-reduce).
- please stop malloc'ing for each token
- prealloc map for found tokens (better to just allocate room for 200k words).
- SIMD would optimise your inner-loop quite a lot. However, there are optimised libraries for this, so you don't have to write this yourself.
- `word = append(word, c)` <= this is very slow
Why is there no profiling? Why don't you check how the compiler interpreted your code and benchmark the subparts?
In addition, there are at least some errors in your optimised program:
- you can't lower-case by substract like you do. Non-ascii characters would fail.
- also you can't tokenising by comparing with c <= ' '. There are many characters which would break a string. See this exercise: https://campus.datacamp.com/courses/introduction-to-natural-...