C++ C++;程序使用<;比特集>;跑得很慢 intmain(){ 双1,零,偏差; 双最大值=-2.0; 字符串明文、密文、firstFourStr、lastFourStr; 位集V; 位集基(“00001110000011”); 位集targetKey; 对于(int k=0;k
可能该代码中最慢的不是C++ C++;程序使用<;比特集>;跑得很慢 intmain(){ 双1,零,偏差; 双最大值=-2.0; 字符串明文、密文、firstFourStr、lastFourStr; 位集V; 位集基(“00001110000011”); 位集targetKey; 对于(int k=0;k,c++,performance,C++,Performance,可能该代码中最慢的不是std::bitsets,而是大量(且不必要)使用std::strings来操作它们。例如: int main(){ double one, zero, bias; double max = -2.0; string plaintext, ciphertext, firstFourStr, lastFourStr; bitset<16> V; bitset<16> base("0000110000001011"); bitse
std::bitset
s,而是大量(且不必要)使用std::string
s来操作它们。例如:
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;
}
位集add2(tmpBs2.to_string()+“000000000000”);
需要从一个位集创建一个std::string
,通过将其与一个常量C字符串连接,生成一个新的std::string
,然后将结果重新解释为std::bitset
最好将
std::bitset
s作为无符号整数处理,并使用位运算符和移位。该代码中最慢的可能不是std::bitset
s,而是大量(且不必要)使用std::string
s来处理它们。例如:
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;
}
位集add2(tmpBs2.to_string()+“000000000000”);
需要从一个位集创建一个std::string
,通过将其与一个常量C字符串连接,生成一个新的std::string
,然后将结果重新解释为std::bitset
最好将
std::bitset
s作为无符号整数处理,并使用按位运算符和移位。如果您在windows上,可以使用Visual Studio Profile Analyzer(或类似的工具,不记得它是如何调用的)。如果您在linux上,您可以将valgrind与callgrind一起使用。这里有探查器,可以向您显示代码花费最多时间的位置。我的感觉是,这里的IO最麻烦。我的意思是您对getline有16 x 16 x 20.000 x 2=10.240.000
调用。如果您在windows上,您可以使用Visual Studio Profile Analyzer(或者类似的东西,不记得它是如何被调用的)。如果你在linux上,你可以将valgrind与callgrind一起使用。这里有分析器,可以告诉你代码花费的时间最多。我的感觉是这里的IO最费力。我的意思是你有16x16x20.000x2=10.240.000
调用getline。