-2

I wrote some code both in C++ and in Java to see which will run faster.

All this code does is it basically increments two variables and then multiplies them. As expected, C++ was slightly faster than Java.

Everything was fine until I changed the data types from int to long (long long in C++). Then C++ took a huge amount of time to execute this code, while there was little increase in execution time for Java.

Does anyone know why it takes so long for C++ to perform calculations on long, compared to Java?

C++ code *(~53 seconds, same result with __int64)* :

long long limit  = 2e9;
long long loopLimitI = sqrt(limit);
long long product = 0;
for(long long i = 2; i < loopLimitI; i++){
    long long loopLimitJ = limit / i;
    for(long long j = 2; j < loopLimitJ; j++){
        product = i * j;
    }
}

Java code (~11 seconds) :

long limit = (long) 2e9;
long loopLimitI = (long) Math.sqrt(limit);
long product = 0;
for(long i = 2; i < loopLimitI; i++){
    long loopLimitJ = limit / i;
    for(long j = 2; j < loopLimitJ; j++){
        product = i * j;
    }
}

EDIT: My OS: Windows 8 (x64). Optimization settings - /O2

Both are running the code, the value of product at the end is 1999923120. Both for C++ and Java

Okay, I just tried creating an array and saving all the results of i * j multiplication (to make sure it is running). I am still getting a huge time for C++, compared with Java.

Any idea why is that happening to me?

SOLUTION:

'Platform' in MS Visual Studio is automatically set to x32. You just have to change it to 'x64'. Sorry for a rather confusing question, I am new to VS and C++.

  • 2
    What is your platform (32 or 64 bits OS)? What are your C++ optimization settings? – syam Jun 10 '13 at 21:47
  • 1
    Did you turn on any optimizations? – Carl Norum Jun 10 '13 at 21:47
  • 1
    Because in the middle of the iteration the JVM started optimizing the behavior of the code. Refer to [How do I write a correct micro-benchmark in Java?](http://stackoverflow.com/q/504103/1065197) in order to handle a **real** benchmark against the Java version of your code. – Luiggi Mendoza Jun 10 '13 at 21:47
  • 1
    The problem is either your optimizations settings, or that you are building 32-bit code (or both.) Of course, as other comments mention, it might be that the JVM is not actually running your code! – yzt Jun 10 '13 at 21:48
  • When I add output and change the limit to 9e7, the [C++](http://ideone.com/81Dh64) ran 50% faster than the [Java](http://ideone.com/YezT6q) – Mooing Duck Jun 10 '13 at 22:00
  • 1
    I suspect the difference is in the code not shown – Mooing Duck Jun 10 '13 at 22:04
  • @Mooing Duck Its basically that inside a function and two variables used to measure the time. Nothing else. Any idea why it takes longer for me? – user2472380 Jun 10 '13 at 22:27
  • @user2472380 you're hiding something. The exact C++ code you've posted runs in less than 1 microsecond on my machine. – Drew Dormann Jun 10 '13 at 22:50
  • @user2472380: _show us the rest of the code_. – Mooing Duck Jun 10 '13 at 23:13
  • 1
    "As expected, C++ was slightly faster than Java" - Why would you expect C++ to run faster? – Steve Kuo Jun 11 '13 at 03:11
  • @Steve Kuo Because it was in most cases (at least for me). – user2472380 Jun 11 '13 at 11:59
  • @SteveKuo has a point. Java is faster in many cases for (long-running, because of the constant JVM start overhead) programs because it can use run-time information for branch prediction and other optimizations, and because it can do whole program optimization (like inlining functions from other translation units). With statically compiled languages one has to perform test runs with instrumented programs and typical inputs to gather run time information first and then perform the actual, optimized compilation again; even today that is not standard. – Peter - Reinstate Monica Sep 01 '17 at 14:49

2 Answers2

2

You compiled it without optimization. Compile with proper settings and the C++ version will run in 0 seconds, because it does nothing.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

This code:

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    long long limit  = 2e9;
    long long loopLimitI = sqrt(limit);
    long long product = 0;
    for(long long i = 2; i < loopLimitI; i++){
        long long loopLimitJ = limit / i;
        for(long long j = 2; j < loopLimitJ; j++){
            product = i * j;
        }
    }
    cout << product;
}

takes 12.1 seconds on my 3.4GHz Athlon64 machine (using g++ 4.6.3 in 64-bit mode - it's slower in 32-bit mode, for natural reasons, since all the 64-bit operations take twice as many steps, it takes just short of twice as long). If I remove the cout << product; line, it takes 0.004s.

If I put the cout line back in and do -funroll-all-loops it takes it down to 4.5s.

I don't have a Java environment to run the Java code, but I don't see believe it would run faster than the C++ code.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks! Yes, it was the 32 bit mode causing the problem. – user2472380 Jun 11 '13 at 11:57
  • `since all the 64-bit operations take twice as many steps` it'll take 4 times as many steps to do a multiplication, and probably much more for other complicated operations – phuclv Dec 12 '16 at 16:05