0

Consider below code:

template<typename T> void f(T a[]) { if(sizeof(T) > 1) for(...)a[i] = j else memset(a, j, ... } 

I think compiler should remove one of the if-branches in compile-time. Is it true?

14
  • depends on compiler and optimization Commented Feb 23, 2012 at 6:55
  • 1
    Why don't you compile it with optimizations with your compiler an see what happens? Commented Feb 23, 2012 at 6:56
  • 4
    If you're trying to optimize copies by using memset() instead of for loops for some type T, sizeof(T) is not an appropriate way to determine if it is eligible for such an optimization. You want to use is_pod<T>::value instead of sizeof(T). Commented Feb 23, 2012 at 6:56
  • 1
    @Volkan Sirin: Depending on your setup, the compiler is probably smarter than you think it is. :-) The only way to be sure is to test and profile. Commented Feb 23, 2012 at 7:16
  • 1
    @VolkanSirin: You think the compiler doesn't implement inlining or something? Usually the person worried most about performance needs it the least. You might want a working program first. Commented Feb 23, 2012 at 7:34

2 Answers 2

1

Since the compiler can decide if (sizeof(T) > 1) will return true or not, at compile time itself, the compiler can emit machine code without any branch if compiled with optimization flag on. So the resultant code will have either for loop (or an equivalent code generated out of it), or memset. In other words, the resultant code will not have if-else block (provided you're using a smart compiler).

Sign up to request clarification or add additional context in comments.

Comments

1

What is wrong with std::fill ?

std::fill(begin(a), end(a), j); 

or are you worried that your Standard Library is not optimized enough ?

3 Comments

we measured that std::fill is significantly slower than above approach
@Volkan: You probably want to check your compiler settings then. It should be almost as fast, if not faster. (For the latest version of all major compilers)
@VolkanSirin: I hate to ask but... did you actually profile optimized code ? FIY I am not sure that the compiler will detect the memset switch, that would perhaps be a good optimization to suggest though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.