Thanks everyone, I can't believe the response I got on this post. I'm a new member, but love this forum already!
sloppy9 - The program you linked last certainly gets around most of the performance hit. Where the original initializer_list code ran in 3.7 times the amount of time the straight up if..else..else code took, your code runs in only 1.8 times. But, there seems to be a bug in the program you linked last, at least when I compile it on gcc 4.7.0 (build 20111106). It is, of course, an experimental source build from their svn repository, so things can happen... If I change your line 55 from "}" to "} else { throw false; }", the program throws false, when that wouldn't be the expected behavior. (Without something like this, it appears to run correctly, but it's just looping and deciding it's never in the set, again, at least on my compiler.)
BTW, I also love your timing method. Much nicer than running ./time <programName> for programs compiled using alternate methods.
I have figured out that the last program you linked is failing on the first iteration of the for loop.
It's driving me nuts, I don't see what's wrong with it. If I ad to the "operatror," function:
for(auto x : il) { cout << x << endl; } // or auto &x
Then I get garbage random output here, including high ascii characters.
Inspired by your variadic template idea, I read up on them, and created a method that doesn't use initializer_list. It runs in the same amount of time that your code does -- about 1.8 times the amount of time the straight up if..else..else code took
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
template<typename T>
inline bool in(const T& compareValue, const T& thisValue) {
return(compareValue == thisValue);
}
template<typename T, typename... Args>
bool in(const T& compareValue, const T& thisValue, const Args&... args) {
return(compareValue == thisValue || in(compareValue, args...));
}
// these are needed because compiler can't sort out in(string, const literal char*, ...)
inline bool in(const string& compareValue, const string& thisValue) {
return(compareValue == thisValue);
}
template<typename... Args>
inline bool in(const string& compareValue, const string& thisValue, const Args&... args) {
return(compareValue == thisValue || in(compareValue, args...));
}
|
I still have to figure out your helper function for type inference idea, so I don't need the second set of functions to handle the string vs char* case.