|
Boost : |
Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2011-09-25 12:27:54
Dave Abrahams wrote:
> No, no, no. Sorry to be so hard-line about this, but no.
Here's a quick benchmark comparing clamp with one template type
parameter vs. three:
#include <iostream>
#include <string>
using namespace std;
#ifdef CLAMP_1_TYPE
template <typename T>
T clamp(const T& val, const T& min, const T& max)
{
if (val<min) return min;
if (val<max) return val;
return max;
}
#else
template <typename T_VAL, typename T_MIN, typename T_MAX>
T_VAL clamp(const T_VAL& val, const T_MIN& min, const T_MAX& max)
{
if (val<min) return min;
if (val<max) return val;
return max;
}
#endif
int main()
{
while (cin.good()) {
string w;
getline(cin,w);
// cout << w << "\n"; // No-op version to measure i/o time
cout << clamp<string>(w,"Aardvark","zebra") << "\n";
}
return 0;
}
Run times reading /usr/share/dict/words and writing to /dev/null are:
No-op: 0.93
3 type clamp: 0.98
1 type clamp: 1.10
So the cost of the extra char*-to-string conversions with the 1-type
version of clamp increases the execution time of that function by more
than 3X.
Personally, I value that performance benefit above the
easier-to-describe semantics that you want.
Regards, Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk