performance - Do compilers optimize if-statements for static values in c++ -
i'm working on generic library class collage assignment, , important code runs efficient possible, if can reduce if-statement should it.
i have following code needed initialize array if fundamental type such double or int.
t b[dim]; if(std::is_fundemental::<t>::value) { memset(b, 0, dim*sizeof(t)); } now question whether check optimized out, such not make runtime check, or need create template initialization specialization fundamental types?
i use g++ c++11, should able run without check on most, preferably all, compilers.
the standard doesn't address optimization, can't imagine compiler wouldn't optimization. matter: you're talking @ 1 or 2 machine instructions, after call function lot more. , formally speaking, doesn't work except integral types. (not i've ever heard of machine double 0 bits wasn't 0.0.)
but fwiw: std::uninitialized_fill_n should @ least fast, , don't need if, since work types.
Comments
Post a Comment