The main performance bottlenecks will be:
- Any call to the I / O function of the stdio file. The fewer calls, the less overhead.
- Dynamic memory allocation. As little as possible should be done. Ultimately, many malloc calls will cause heap segmentation.
So it comes down to the classic consideration of programming: you can get either fast execution time, or you can get low memory usage. You cannot get both, but you can find a suitable middle ground that is most effective in terms of both runtime and memory consumption.
In extreme cases, the fastest execution can be obtained by reading the entire file as one large piece and loading it into dynamic memory. Or, as a last resort, you can read it byte by byte and evaluate it when reading, which can make the program slower, but will not use dynamic memory at all.
To optimize the code, you will need fundamental knowledge of various processor-specific and OS-specific functions. All issues, such as alignment, cache layout, efficiency of basic functions of API functions, etc. Etc. Will make a difference.
Why not try a few different ways and compare them?
source share