Boost logo

Boost Users :

From: Darin Adler (darin_at_[hidden])
Date: 2001-12-14 12:06:48


On 12/14/01 8:04 AM, Rodney.Schuler_at_[hidden] wrote:

> I saw the boost preprocessor library and I thought that
> BOOST_PREPROCESSOR_MIN might help solve a problem in some code that I am
> maintaining.

The PREPROCESSOR library is intended for "preprocessor metaprogramming". You
use the library to write "preprocessor programs" that generate code. The
parameters to the BOOST_PREPROCESSOR_MIN macro must be things that can be
evaluated at preprocessor time.

You'll get a compiler error if you try to pass something like "func(1)" to
BOOST_PREPROCESSOR_MIN. On the other hand, if you invoke
BOOST_PREPROCESSOR_MIN(0, 1), you are guaranteed to get a 0, with no
run-time overhead.

> This code has the naive implementation of MIN and MAX:
> #define MIN(a,b) ((a)<(b)?(a):(b))
> This code also has many (tens of thousands!) of places where a function
> calls are used as arguments to MIN and MAX like this:
> foo=MIN(func(args1),func(args2))
> The function called is a very long running function. The performance hit
> taken by three evaluations of the function is a problem I am working to
> fix. I know the best solution is to add some local variables, and store
> the results of the functions in them, then use MIN on the local vars. But
> I'd like to get some Idea of how much performance increase I am likely to
> get by crawling through the code and fixing all the occurrences of
> MIN(func(args1),func(args2))

I suggest you create a min function, and use it instead of the MIN macro.
You could define it like this:

    int min(int a, int b) { return a < b ? a : b; }

Then overload as necessary for the types needed in your program. And change
the calls from MIN(func(args1),func(args2)) to min(func(args1),func(args2)).

You can also mark the min functions "inline" if you find that makes your
program faster or smaller. You can use a function template if you need a
function like min for many different types.

This is *not* a job for the PREPROCESSOR library.

    -- Darin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net