- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
O.h
----------
enum kind { C,P };
template
static double p(const double S,const double X);
};
template
inline double O
{
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,
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I suspect that the following is what you actually want to do
template
inline double O
{
return (X-S>0.0?X-S:0.0);
}
template <>
inline double O
{
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page