我针对 C ++ (在Visual Studio Community Edition 2015中实现)和 Java (1.7)for循环进行了一些性能测试。
以下是源代码:
爪哇:
long startTime = 0;
long endTime = 0;
long totalTime = 0;
startTime = System.currentTimeMillis();
for (long counter = 0; counter < numberOfIterations; counter++)
{
System.out.println("01234");
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
C ++(基于Windows,发布版本x64,针对速度进行了优化):
ULONGLONG startTime = 0;
ULONGLONG endTime = 0;
ULONGLONG elapsedTime = 0;
startTime = GetTickCount64();
for (LONGLONG counter = 0; counter < numberOfIterations; counter++)
{
cout << "01234" << endl;
}
endTime = GetTickCount64();
elapsedTime = endTime - startTime;
当我旋转/循环它们100,000次时,结果真的让我感到惊讶。
他们来了:
的爪哇
C ++:
然后我有另一套测试用例
爪哇:
long startTime = 0;
long endTime = 0;
long totalTime = 0;
startTime = System.currentTimeMillis();
for(long counter = 0; counter < numberOfIterations; counter++) {
String tempString = new String("test");
}
endTime = System.currentTimeMillis();
C ++(基于Windows,发布版本x64,针对速度进行了优化):
ULONGLONG startTime = 0;
ULONGLONG endTime = 0;
ULONGLONG elapsedTime = 0;
startTime = GetTickCount64();
for (LONGLONG counter = 0; counter < numberOfIterations; counter++)
{
string tempString = "test";
}
endTime = GetTickCount64();
elapsedTime = endTime - startTime;
同样,当我旋转/循环它们10,000,000次时,结果真的更令人惊讶。 他们来了: 的爪哇
C ++:
但是在空循环C ++上。
在进行此测试之前,我实际上认为C ++总是在低级别或特定于OS / Platform的实现中执行Java。 但是,在这种情况下,这是否意味着Java具有更有效的方式来处理字符串,如果它已经在卷中?
感谢
答案 0 :(得分:2)
最后,我能够按照我的承诺,抽出时间在这里发布答案。但我为此道歉。无论如何,这里是我收集的统计数据。请耐心等待,这是一个很长的答案。顺便说一下,它们都是在Windows 10 Pro x64 Machine =)中执行的!
第一个代码(包括C ++和Java): 在Windows上的Java中:
public void testForLoopCreateInt(long numberOfIterations) {
ArrayList<Integer> listOfIntegers = new ArrayList<Integer>();
long startTime = 0;
long endTime = 0;
long totalTime = 0;
System.out.println("\n===========================" + "\ntestForLoopCreateInt() Looping for: " + numberOfIterations);
startTime = System.currentTimeMillis();
for(long counter = 0; counter < numberOfIterations; counter++) {
int i = 0;
listOfIntegers.add(i);
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println("Total time: " + totalTime + " milliseconds");
System.out.println("===========================testForLoopCreateInt()");
for (int indexer = 0; indexer < 10; indexer++) {
int y = listOfIntegers.get(indexer);
}
}
在C ++中通过某些Win32 API:
void Loops::testForLoopCreateInt(LONGLONG numberOfIterations)
{
cout << "\n===========================" << "\ntestForLoopCreateInt() Looping for: " << numberOfIterations << endl;
vector<int> vectorOfInts;
high_resolution_clock::time_point startTime = high_resolution_clock::now();
for (LONGLONG counter = 0; counter < numberOfIterations; counter++)
{
int i = 0;
vectorOfInts.push_back(i);
}
high_resolution_clock::time_point endTime = high_resolution_clock::now();
duration<double, std::milli> totalTime = endTime - startTime;
cout << "Total time: " << totalTime.count() << " milliseconds" << endl;
cout << "===========================testForLoopCreateInt()" << endl;
for (int indexer = 0; indexer < 10; indexer++) {
int y = vectorOfInts.at(indexer);
}
}
当每个迭代次数设置为某个值时,结果如下:
<强>爪哇:强>
<强> C ++:强>
第二个代码(包括C ++和Java): 在Windows上的Java中:
public void testForLoopCreateUniformStringAndStoreToArrayList(long numberOfIterations) {
ArrayList<String> listOfIntegers = new ArrayList<String>();
long startTime = 0;
long endTime = 0;
long totalTime = 0;
System.out.println("\n===========================" + "\ntestForLoopCreateUniformStringAndStoreToArrayList() Looping for: " + numberOfIterations);
startTime = System.currentTimeMillis();
for(long counter = 0; counter < numberOfIterations; counter++) {
String string = new String("01234");
listOfIntegers.add(string);
}
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
System.out.println("Total time: " + totalTime + " milliseconds");
System.out.println("===========================testForLoopCreateUniformStringAndStoreToArrayList()");
for (int indexer = 0; indexer < 10; indexer++) {
String y = listOfIntegers.get(indexer);
}
}
在C ++中使用某些Win32 API:
void Loops::testForLoopCreateUniformStringAndStoreToVector(LONGLONG numberOfIterations)
{
cout << "\n===========================" << "\ntestForLoopCreateUniformStringAndStoreToVector() Looping for: " << numberOfIterations << endl;
vector<string> vectorOfStrings;
high_resolution_clock::time_point startTime = high_resolution_clock::now();
for (LONGLONG counter = 0; counter < numberOfIterations; counter++)
{
string str000("01234");
vectorOfStrings.push_back(str000);
}
high_resolution_clock::time_point endTime = high_resolution_clock::now();
duration<double, std::milli> totalTime = endTime - startTime;
cout << "Total time: " << totalTime.count() << " milliseconds" << endl;
cout << "===========================testForLoopCreateUniformStringAndStoreToVector()" << endl;
for (int indexer = 0; indexer < 10; indexer++) {
string y = vectorOfStrings.at(indexer);
}
}
当每个迭代次数设置为某个值时,结果如下:
<强>爪哇:强>
<强> C ++:强>
所以这些是结果,我不确定这是否有偏见但是我正在尽可能公平对待C ++(在Windows上)对Java(在Windows上)。所以你就是法官。
感谢。