爲什麼Java中的代碼比С++更快? 我需要逐字節比較2個文件。 例如,當比較兩個文件大小650mb需要40秒到C++和10秒的Java。比較兩個文件或爲什麼Java中的代碼比С++更快?
C++代碼:
//bufferSize = 8mb
std::ifstream lFile(lFilePath.c_str(), std::ios::in | std::ios::binary);
std::ifstream rFile(rFilePath.c_str(), std::ios::in | std::ios::binary);
std::streamsize lReadBytesCount = 0;
std::streamsize rReadBytesCount = 0;
do {
lFile.read(p_lBuffer, *bufferSize);
rFile.read(p_rBuffer, *bufferSize);
lReadBytesCount = lFile.gcount();
rReadBytesCount = rFile.gcount();
if (lReadBytesCount != rReadBytesCount ||
std::memcmp(p_lBuffer, p_rBuffer, lReadBytesCount) != 0)
{
return false;
}
} while (lFile.good() || rFile.good());
return true;
和Java代碼:
InputStream is1 = new BufferedInputStream(new FileInputStream(f1));
InputStream is2 = new BufferedInputStream(new FileInputStream(f2));
byte[] buffer1 = new byte[64];
byte[] buffer2 = new byte[64];
int readBytesCount1 = 0, readBytesCount2 = 0;
while (
(readBytesCount1 = is1.read(buffer1)) != -1 &&
(readBytesCount2 = is2.read(buffer2)) != -1
) {
if (Arrays.equals(buffer1, buffer2) && readBytesCount1 == readBytesCount2)
countItr++;
else {
result = false
break;
}
}
,如果你做的緩衝區大小相同,會發生什麼? – Xymostech 2013-03-02 17:20:06
您是否將文件緩存作爲可能的影響因素?當在C++中緩存相同的代碼的工作時間更長時,則爲 – NPE 2013-03-02 17:20:38
。 – Silnet 2013-03-02 17:32:33