I am currently working on a small bash script that reads source C files and then finds corresponding include files.
Now I have to choose between two models of finding a mapping M: filename -> {include file paths }. What I have are two arrays – one with found include filenames and another containing possible paths. The options are :
a) look for each file in one path, then proceed to another path;
b) look through a list of paths to find one file, then proceed to another file.
My question is – what will be faster? There is equal number of iterations in each of the choices, but maybe some of the commands are more time-consuming? I personally prefer option a).
Thanks for any help and sorry if this was a stupid question.
There would be the same amount of iterations only if you coded it badly, not using the
continuekeyword once a match is found (don’t forget it can take a number as an argument to affect outer loops too).I would iterate through all the include files and look into the paths with some ordering –
<system>includes should search system include paths first for quicker results, while the same holds true for"local"includes. It would be unwise to discard this information while parsing.However, I think you can do better by just running
find -type fon all the include paths and storing the result in a temporary file or a variable. Then you would justgrep(or any equivalent) it for the includes themselves. This way you save some I/O, since half of it is read in one go and only once.