Next: 6.15 Calling VIPS functions Up: 6. Programming Previous: 6.13 The _Object class Contents

#
6.14 Optimisation

`nip2` performs three useful optimisations on expressions. First, it finds and removes common sub-expressions in functions. So for example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `if a + b < 12 then a + b else b` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

will only evaluate `a + b` once. This can save a lot of time if `a` or `b` is a large image.

Second, `nip2` detects arithmetic operations on `unsigned char` images, and replaces them with look-up tables. For example:

> | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` | `` |  | `` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `a = vips_image "campin.v"` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
> | `b = a * (a - 1) ** 0.5` |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |

Provided `campin.v` is an 8 bit image image, this expression will evaluate with a single call to `im_maplut()`.

Finally, `nip2` has a VIPS operation cache. It memorises the arguments to the last few hundred calls to VIPS, and the result each call gave. Before calling VIPS again, it checks to see if there is a previous call with the same arguments and if there is, uses the result it obtained last time.

Next: 6.15 Calling VIPS functions Up: 6. Programming Previous: 6.13 The _Object class Contents