Which Invocation Would Complete Its Execution More Quickly?

Question:

Given an implementation of fw implemented using an ordinary binary tree and the two invocations:

a) % fw /usr/dict/words /usr/man/*/*

b) % fw /usr/man/*/* /usr/dict/words

Which would you expect to complete its execution more quickly and why?

Answer:

The invocation % fw /usr/dict/words /usr/man/*/* is expected to complete its execution more quickly because it starts with a smaller path size before traversing a larger path size.

Explanation:

The Path Order Matters:

When the paths are specified as /usr/dict/words /usr/man/*/*, the first path specified is a single file (/usr/dict/words) and the second path is a wildcard path (/usr/man/*/*) which matches multiple files. In this case, the algorithm traverses the tree using the smaller path size (1 file) first and then traverses the larger path size (multiple files).

On the other hand, when the paths are specified as /usr/man/*/* /usr/dict/words, the first path (/usr/man/*/*) matches multiple files and the second path is a single file (/usr/dict/words). In this case, the algorithm needs to traverse the tree for all the matched files before moving on to the single file, which will take more time.

Therefore, the order of paths in the invocation affects the execution time of the implementation utilizing an ordinary binary tree.

← A hoop and a cylinder rolling down a ramp Preventing dumpster diving risks in your company →