Reputation: 1003
I have made some performance test against C++ (implemented in Visual Studio Community Edition 2015) and Java (1.7) for loops.
The following are the source code:
Java:
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 based, release version x64, optimized for speed):
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;
The results really surprised me when I spin/loop them for 100,000 times.
Here they are:
Java:
C++:
Then I had another set of test case
Java:
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 based, release version x64, optimized for speed):
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;
Again, the results is really more surprising, when I spin/loop them for 10,000,000 times. Here they are: Java
C++:
But on empty loops C++.
Before doing this test I was actually thinking that C++ will always out perform Java in low level or OS/Platform specific implementation. But, in this case, does this mean that Java has more efficient way of handling Strings specially if it is already in volumes?
Thanks
Upvotes: 3
Views: 228
Reputation: 1003
Finally, I was able to make time to post an answer here as I promised. But my apologies for that. Anyways, here are the stats I have gathered. Please bear with me, this is quite a lengthy answer. By the way, both of them were executed in Windows 10 Pro x64 Machine =)!
First code (both C++ and Java): In Java on Windows:
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);
}
}
In C++ over some 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);
}
}
When each of these had number of iterations set to some value the following are the results:
Java:
C++:
Second code (both C++ and Java): In Java on Windows:
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);
}
}
In C++ over Some 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);
}
}
When each of these had number of iterations set to some value the following are the results:
Java:
C++:
So these are the results, I am not sure if this is biased or not but I am making this as fair as I can to both C++ (on windows) against Java (on windows). So you'd be the judge.
Thanks.
Upvotes: 1