C / C ++:正片叠底,或bitshift然后分? [英] C/C++: Multiply, or bitshift then divide?

查看:456
本文介绍了C / C ++:正片叠底,或bitshift然后分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有可能的话,我想知道如果它的速度更快了bitshift后跟一个整数除法来取代单一的乘法。说我有一个int k和我想通过2.25乘以它。

Where it's possible to do so, I'm wondering if it's faster to replace a single multiplication with a bitshift followed by an integer division. Say I've got an int k and I want to multiply it by 2.25.

什么是快?

int k = 5;
k *= 2.25;
std::cout << k << std::endl;

int k = 5;
k = (k<<1) + (k/4);
std::cout << k << std::endl;

输出

11
11

两者给出相同的结果,你可以检查这个完整的例子

推荐答案

我定义的函数 regularmultiply() bitwisemultiply()如下:

int regularmultiply(int j)
{
    return j * 2.25;
}

int bitwisemultiply(int k)
{
    return (k << 1) + (k >> 2);
}

一旦(在2009年的MacBook OS X 10.9.2中的X code)做分析与仪器,它似乎是 bitwisemultiply 比<$执行约快2倍C $ C> regularmultiply 。

组装code输出似乎证实了这一点,用 bitwisemultiply 花费其大部分时间在册洗牌和函数返回,而 regularmultiply 度过大部分时间的乘法。

The assembly code output seemed to confirm this, with bitwisemultiply spending most of its time on register shuffling and function returns, while regularmultiply spent most of its time on the multiplying.

regularmultiply

bitwisemultiply

但我的审判的时间太短。

But the length of my trials was too short.

接着,我试图执行两种功能拥有1000万次乘法,而这一次把循环中的功能,使所有的函数入口和离开不会混淆的数字。而这个时候,结果是每个方法都用了大约52毫秒的时间。所以至少在一个比较大的,但不是庞大数目的计算,这两个函数取大约相同的时间。这让我很吃惊,所以我决定来计算时间更长,更大的数字。

Next, I tried executing both functions with 10 million multiplications, and this time putting the loops in the functions so that all the function entry and leaving wouldn't obscure the numbers. And this time, the results were that each method took about 52 milliseconds of time. So at least for a relatively large but not gigantic number of calculations, the two functions take about the same time. This surprised me, so I decided to calculate for longer and with larger numbers.

这一次,我只乘以亿通5亿2.25,但 bitwisemultiply 居然出来比稍慢 regularmultiply

This time, I only multiplied 100 million through 500 million by 2.25, but the bitwisemultiply actually came out slightly slower than the regularmultiply.

最后,我切换两个功能的顺序,只是为了看看在仪器日益增长的CPU图形也许拖慢的第二功能。但尽管如此,在 regularmultiply 表现稍好:

Finally, I switched the order of the two functions, just to see if the growing CPU graph in Instruments was perhaps slowing the second function down. But still, the regularmultiply performed slightly better:

下面是最后的程序是什么样子:

Here is what the final program looked like:

#include <stdio.h>

int main(void)
{
    void regularmultiplyloop(int j);
    void bitwisemultiplyloop(int k);

    int i, j, k;

    j = k = 4;
    bitwisemultiplyloop(k);
    regularmultiplyloop(j);

    return 0;
}

void regularmultiplyloop(int j)
{
    for(int m = 0; m < 10; m++)
    {
        for(int i = 100000000; i < 500000000; i++)
        {
            j = i;
            j *= 2.25;
        }
        printf("j: %d\n", j);
    }
}

void bitwisemultiplyloop(int k)
{
    for(int m = 0; m < 10; m++)
    {
        for(int i = 100000000; i < 500000000; i++)
        {
            k = i;
            k = (k << 1) + (k >> 2);
        }
        printf("k: %d\n", k);
    }
}

结论

那么,我们可以说这一切?有一件事我们可以肯定地说是优化编译器的比大多数人好的。进而,这些优化展现自己更当有一个的很多的计算,这是你真的想反正优化唯一的一次。所以,除非你在编码的装配优化,改变乘法位转移可能不会太大帮助。

Conclusion

So what can we say about all this? One thing we can say for certain is that optimizing compilers are better than most people. And furthermore, those optimizations show themselves even more when there are a lot of computations, which is the only time you'd really want to optimize anyway. So unless you're coding your optimizations in assembly, changing multiplication to bit shifting probably won't help much.

这是一件好事,想想你的应用程序的效率,但微观效率收益通常不足以保证让您code的可读性。

It's always good to think about efficiency in your applications, but the gains of micro-efficiency are usually not enough to warrant making your code less readable.

这篇关于C / C ++:正片叠底,或bitshift然后分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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