Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7957 Discussions

c++ template inline method, optimization

finjulhich
Beginner
462 Views

O.h
----------

enum kind { C,P };

template class O {
static double p(const double S,const double X);
};

template
inline double O::p(const double S,const double X)
{
return (k==C)? (S-X>0.0?S-X:0.0) : (X-S>0.0?X-S:0.0);
}

//explicit template instatiation in translation unit1
template class O;
template class O

;

and use of inline p() function in tr.unit 2 (which #include O.h)

p() is called millions of times.

As I understand, intel c++ compilers (an others), doesn't generate during templ instantiations resulting c++ code and then feeds to template-less compilers.

Basically, when i use the same code with O

. It's a lot faster than with O.
Should i assume that k==C test is optimized away before compilation?

What is the best way to rewrite p()? Using std::max(S-X,0.0) was slower.

rds,

0 Kudos
1 Reply
Thomas_W_Intel
Employee
462 Views

Hello,

I suspect that the following is what you actually want to do

template
inline double O::p(const double S,const double X)
{
return (X-S>0.0?X-S:0.0);
}

template <>
inline double O::p(const double S,const double X)
{
return (S-X>0.0?S-X:0.0);
}

If the template argument is "P", then the general implementation is used--the first one.

If the template argument is "C", then the specialization is used--the second implementation.

Using the template mechanism, you can ensure that the decision, which implementation to use, is taken at compile time.

Kind regards

Thomas

0 Kudos
Reply