简体   繁体   English

更快的二进制加法C ++

[英]Faster Binary Addition C++

I want to improve the speed of my current binary addition problem. 我想提高当前二进制加法问题的速度。 What it does is create 2 vectors with size K and to the first adds 1. Maybe it can't be faster but if it's possible please let me know. 它的作用是创建2个大小为K的向量,并且第一个添加1.也许它不能更快​​但是如果可能的话请告诉我。

Edit: Modified for changing const vector& a, const vector& b 编辑:修改为修改const向量&a,const向量&b

#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <vector>

using namespace std;

vector<int> BinaryAddition(const vector<int>& a, const vector<int>& b, int tam){
    vector<int> c(tam);
    int ac = 0;

    for(int i=tam-1; i>-1; i--){
        c[i] = ((a[i] ^ b[i]) ^ ac); //a xor b xor c
        ac = ((a[i] & b[i]) | (a[i] &ac)) | (b[i] & ac); 
    }

    return c;
}

/* retorna "a - b" en segundos */
double performancecounter_diff(LARGE_INTEGER *a, LARGE_INTEGER *b)
{
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    return (double)(a->QuadPart - b->QuadPart) / (double)freq.QuadPart;
}

int main(int argc, char *argv[])
{
    LARGE_INTEGER t_ini, t_fin;
    double secs;

    QueryPerformanceCounter(&t_ini);

    int k=15;

    vector<int> uno1 (k,0);
    vector<int> pro (k,0);
    vector<int> pro1(k,0);

    uno1[k-1] = 1;

    pro1 = BinaryAddition(pro, uno1, k);

    QueryPerformanceCounter(&t_fin);

    secs = performancecounter_diff(&t_fin, &t_ini);
    printf("%.16g milliseconds\n", secs * 1000.0);

    return 0; 
}

First of all, this: 首先,这个:

vector<int> BinaryAddition(vector<int> a, vector<int> b, int tam)

should be: 应该:

vector<int> BinaryAddition(const vector<int>& a, const vector<int>& b, int tam)

You are copying the input parameters vectors for no reason, pass them by reference rather than by value, which requires copying. 您无缘无故地复制输入参数向量,通过引用而不是值传递它们,这需要复制。

Another thing you could try which may improve the speed is a simple technique called loop unwinding (or unrolling) This will certainly not make your code more readable or prettier, but may actually speed it up a bit - but do compare it to the simple version compiled with maximum optimization (usually compiler's option -O3 ), cause it may happen that your compiler already does the same optimization (or a different one, with a better effect). 您可以尝试的另一件可以提高速度的方法是一种称为循环展开(或展开)的简单技术。这肯定不会使您的代码更具可读性或更漂亮,但实际上可能会加快一点 - 但请将它与简单版本进行比较使用最大优化(通常是编译器的选项-O3 )编译,导致您的编译器可能已经执行相同的优化(或者不同的优化,效果更好)。

I just did a quick hack to make the obvious solution: 我只是做了一个快速的黑客来做出明显的解决方案:

vector<int> BinaryAddition3(const vector<int> &a, const vector<int> &b, int tam){
    vector<int> c(tam);
    int ac = 0;

    for(int i=tam-1; i>-1; i--){
       int t = a[i]+b[i] + ac;
       ac = t > 1;
       c[i] = t & 1;
    }

    return c;
}

This is actually a fraction slower than the less clear xor/or variant posted in the original question - about 0.05ms slower. 这实际上比原始问题中发布的不太清晰的xor /或变体慢一点 - 慢约0.05ms。 However, that is measuring only the actual add, not the whole vector, and for a binary number that is 35000 integers long - and it still only takes 0.1 ms per addition on my rather ancient AMD quad core processor. 然而,这只是测量实际的添加,而不是整个矢量,并且对于35000整数长的二进制数 - 并且在我相当古老的AMD四核处理器上每次添加仍然只需要0.1毫秒。

In my testing, the creation/initialization of the array takes about half the total time as the measurement of the total time. 在我的测试中,阵列的创建/初始化占总时间的一半,作为总时间的测量。 Adding const reference makes it about twice as fast for the actual addition function. 添加const引用使其大约是实际添加功能的两倍。 This is definitely faster than the ORIGINAL function, but like I said, it's marginally slower - but clearer. 这肯定比ORIGINAL函数更快,但就像我说的那样,它稍微慢一些 - 但更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM