gpt4 book ai didi

使用 的 C++ 程序运行速度很慢

转载 作者:太空宇宙 更新时间:2023-11-04 16:11:25 24 4
gpt4 key购买 nike

int main(){
double one, zero, bias;
double max = -2.0;
string plaintext, ciphertext, firstFourStr, lastFourStr;
bitset<16> V;
bitset<16> base("0000110000001011");
bitset<16> targetKey;
for(int k=0; k<16; k++){
ifstream plaintexts ("plaintexts.txt");
ifstream ciphertexts ("ciphertext07.txt");
bitset<12> tmpBs(k);
bitset<16> add(tmpBs.to_string()+"0000");
bitset<16> guessedKey1(add^base);
for(int j=0; j<16; j++){
bitset<4> tmpBs2(j);
bitset<16> add2(tmpBs2.to_string()+"000000000000");
bitset<16> guessedKey(guessedKey1^add2);
one = zero = 0.0;
for(int i=0; i<20000; i++){
getline(plaintexts, plaintext);
getline(ciphertexts, ciphertext);
bitset<16> pTxt(plaintext);
bitset<16> cTxt(ciphertext);
V = guessedKey^cTxt;
bitset<4> firstFour((V.to_string()).substr(0,4));
bitset<4> secondFour((V.to_string()).substr(4,4));
bitset<4> thirdFour((V.to_string()).substr(8,4));
bitset<4> lastFour((V.to_string()).substr(12,4));
bitset<16> U(sBoxInverse(firstFour)+sBoxInverse(secondFour)+sBoxInverse(thirdFour)+sBoxInverse(lastFour));
if((U[2]^U[6]^U[10]^U[14]^pTxt[4]^pTxt[7]^pTxt[12]^pTxt[15]) == 0) zero++;
else one++;
}
plaintexts.close();
ciphertexts.close();
bias = zero/(zero+one)-0.5;
if(bias < 0) bias *= -1;
if(max <= bias){
max = bias;
targetKey = guessedKey;
}
cout << bias << endl;
}
}
cout << targetKey << ": " << max << endl;
}

我正在做密码学作业,这是我为获取 key 而编写的程序。但它运行得很慢。我试图用更简单的代码替换一些代码行,以检查是否像 to_string() 或 set() 这样的操作导致了问题,但似乎它们与问题无关。那么是什么原因导致程序运行缓慢呢?

最佳答案

可能该代码中最慢的不是 std::bitset,而是广泛(且不必要)使用 std::string操纵他们。例如:

 bitset<16> add2(tmpBs2.to_string()+"000000000000");

需要从一个 bitset 创建一个 std::string,通过将它与常量 C 字符串连接来创建一个新的 std::string,然后将结果重新解释为一个 std::bitset

std::bitset 作为无符号整数并使用按位运算符和移位会更好。

关于使用 <bitset> 的 C++ 程序运行速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28265621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com