Optimisation of division in gcc

前端 未结 4 1029
無奈伤痛
無奈伤痛 2021-01-17 19:43

Here\'s some code (full program follows later in the question):

template 
T fizzbuzz(T n) {
    T count(0);
    #if CONST
        const T d         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 19:51

    The difference in speed is caused by the compiler not knowing if "div" will change value. When it is non-const, it is treating it like a variable being passed in. It could be anything, and so the compiler will use an instruction that divides two variables - idivl. When you say that it's const, the compiler is free to treat it exactly as if you'd typed:

    if (i  % 3 == 0)
    

    I'm kind of surprised that it didn't use bitwise AND(&).

    The WrappedInt isn't being optimized because, well, its not an int. Its a class.

    Something that you could do is incorporate fizzbuzz into WrappedInt.

提交回复
热议问题