Question: 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.

Answer: 
What you should do depends on your goal, which I don't quite understand. You probably should use makedepend, as mentioned in an earlier answer, if you are creating `make` files.

If instead you want to find out if there are multiple versions of header files on your system, consider using the `locate` command. For example, on my ubuntu 12.04 linux system, the following command takes about a second to complete, and produces the output shown below the command.

```
  $ locate stdio.h | grep usr/include
  /usr/include/stdio.h
  /usr/include/c++/4.6/tr1/stdio.h
  /usr/include/glib-2.0/glib/gstdio.h
  /usr/include/sharutils/stdio.h
  /usr/include/x86_64-linux-gnu/bits/stdio.h

```

If you have a list of library paths on separate lines of file pqr, and a list of header files in file xyz, you can use a command like

```
  locate / | grep -f xyz | grep -f pqr

```

(Note, `-F` could be used along with `-f`, if you like.) With two lines (`stdio.h` and `stdlib.h`) in `xyz` and two lines (`/usr/include/` and `/usr/lib/`) in `pqr`, the command takes .7 seconds on my system, to produce the output

```
  /usr/include/stdio.h
  /usr/include/stdlib.h
  /usr/include/c++/4.6/tr1/stdio.h
  /usr/include/c++/4.6/tr1/stdlib.h
  /usr/include/freetype2/freetype/config/ftstdlib.h
  /usr/include/glib-2.0/glib/gstdio.h
  /usr/include/sharutils/stdio.h
  /usr/include/sharutils/stdlib.h
  /usr/include/x86_64-linux-gnu/bits/stdio.h
  /usr/include/x86_64-linux-gnu/bits/stdlib.h
  /usr/lib/perl/5.14.2/CORE/nostdio.h
  /usr/lib/syslinux/com32/include/stdio.h
  /usr/lib/syslinux/com32/include/stdlib.h

```

Note, on a typical linux system, the file name database updates once a day. That probably won't be a problem when looking for system header files. For more-transient files, you might need to say `sudo updatedb` before using `locate`, and that will add a few seconds to a few minutes to the overall time.
To find your include files, a dedicated solution would be [makedepend](http://en.wikipedia.org/wiki/Makedepend), in particular if you are using makefiles anyway.