我想提高当前二进制加法问题的速度。它所做的是创建 2 个大小为 K 的向量,并在第一个向量上添加 1。也许它不能更快,但如果可能的话,请告诉我。
编辑:修改为更改 const vector& a, const vector& 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;
}