C ++中的循环优化 [英] loop optimisation in c++

查看:88
本文介绍了C ++中的循环优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在下面的代码中进一步优化for循环?还有没有一种方法可以避免在循环内进行声明,即int x0,x1等?

How can i further optimise the for loop in the codes below?is there also a way to avoid declarations inside the loop ie int x0,x1, etc?

void tom::add(void* btr)
{

      __declspec(align(16))short* b =(short*)btr;
     int j;

    
        
        for(j = 0; j < 16; j += 4)
        {
            /// 1st stage transform.
            int x0 = (int)(b[j]     + b[j+3]);
            int x3 = (int)(b[j]     - b[j+3]);
            int x1 = (int)(b[j+1] + b[j+2]);
            int x2 = (int)(b[j+1] - b[j+2]);

            /// 2nd stage transform.
            b[j]        = (short)(x0 + x1);
            b[j+2]  = (short)(x0 - x1);
            b[j+1]  = (short)(x2 + (x3 << 1));
            b[j+3]  = (short)(x3 - (x2 << 1));
        }

推荐答案

是的,将声明放在循环之外.

通过不强制循环,您还可以节省大量时间.如果需要int进行计算,请在开始循环之前将传入的参数强制转换为int,进行循环,然后在循环之后将结果强制转换回short.
Yeah, put the declarations outside the loop.

You''d also save a lot of time by not casting in the loop. If you need an int for the calculations, cast the incoming parameter to an int before you start the loop, do the loop, and then cast the result back to a short after the loop.


在循环内声明int不会影响性能.输入函数时,将在堆栈上完成对该对象的分配.另外一个int没有构造函数,这是至关重要的部分.声明一个int不会生成任何代码.

具有非平凡的构造函数的对象是另一个问题,在处理需要性能的循环时应特别注意.

另外,请注意此类语句中的溢出
Declaring the int''s inside the loop does not affect performance. The allocation for that object is done on the stack when you enter the function. Also an int has no constructor, which is the crucial part. Declaring an int does not generate any code.

Objects having non-trivial constructors is another issue, and should be given special concern when dealing with loops that requires performance.

Also, watch out for overflow in statements like this
(int)(b[j] + b[j+3]);



如果没有更好的事情要做,也可以展开循环.


您的代码还有其他困扰我的地方.如果仅接受实现的short*,为什么要使用void*作为函数的参数?以short*作为参数并让可能知道您的void指针的内容比add()函数要好得多的调用者进行转换会更好吗?



If you have nothing better to do, you could also unroll the loop.


There is something else bothering me with your code. Why do you use a void* as argument to your function if you only accept a short* implementing it? Would it not be better to have a short* as argument and let the caller, who probably knows your void pointers content a lot better than the add() function, do the conversion?


这篇关于C ++中的循环优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆