c++ - How to do this conditional compilation 'elegantly'? -


i have code needs run fast , optimizing heck out of inner loop run several hundred trillion times.

in pursuit of this, have been writing several different versions of code in inner loop, using naive methods, using sse intrinsics, etc etc. did of idea when run it on particular hardware combination run test, see implementation / compiler commands combination worked best , run it.

at first when 2 different methods used simple conditional compilation inside loop follows

do {     #ifdef naive_loop     //more code here     #endif     #ifdef partially_unrolled_loop     //more code here     #endif } while( runnumber < maxrun ); 

later number of variations , different things tried grew, turned this:

#ifdef naive_loop void calcrunner::loopfunction() { //code goes here } #endif #ifdef partially_unrolled_loop void calcrunner::loopfunction() { //code goes here } #endif #ifdef sse_intrinsics void calcrunner::loopfunction() { //code goes here } #endif //etc 

however making file become enormous , annoying read. there more elegant way this?

you can use template , template specialization job. example:

template <typename t> class calcrunner;  template <> class calcrunner<naive_loop> {    void loopfunction(void){...} };  template <> class calcrunner<partially_unrolled_loop> {    void loopfunction(void){...} };  // instantiate wanna @ compiler time  typename calcrunner<partially_unrolled_loop> calcrunner_t  int main() {    calcrunner_t runner;    runner.loopfunction(); } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -