Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:58:22+00:00 2026-05-25T20:58:22+00:00

I did a simple test, I know C++ is faster but the results of

  • 0

I did a simple test, I know C++ is faster but the results of my test is unrealistic.

C++ code is:

#include <stdio.h>
#include <windows.h>

unsigned long long s(unsigned long long n)
{
    unsigned long long s = 0;

    for (unsigned long long i = 0; i < n; i++)
        s += i;

    return s;
}

int main()
{
    LARGE_INTEGER freq, start, end;
    QueryPerformanceFrequency(&freq);
    QueryPerformanceCounter(&start);

    printf("%llu\n", s(1000000000));

    QueryPerformanceCounter(&end);
    double d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;

    printf("Delta: %f\n", d);

    return 0;
}

Java code is:

public class JavaApplication5 {

    public static long s(long n) {
        long s = 0;

        for (long i = 0; i < n; i++) {
            s += i;
        }

        return s;
    }

    public static void main(String[] args) {

        long start = System.nanoTime();

        System.out.println(s(1000000000));

        long end = System.nanoTime();

        System.out.println((end - start)/1000000);
    }
}

C++ compiler: gcc 4.4.0 and Java: jdk 1.6.0

Java: 2795 ms

C++ : 0.013517 ms

It says C++ is 206777 times faster than Java! No way! What is wrong in my test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:58:23+00:00Added an answer on May 25, 2026 at 8:58 pm

    Show the compiler options you used. And your REAL code (#include <stdio> isn’t your real code).

    Your C++ compiler is much smarter than your Java compiler (this is true on average and in your case, but not every C++ compiler is smarter than every Java compiler), and it precomputed the result. The only thing you’re timing is the printf call.

    On most of the tasks Java is used for, it performs about as well as C++.

    VM languages (Java, C#) have additional costs related to JIT compilation, but also benefit from more efficient memory allocation and inlining across shared libraries. And C++ is much much faster at accessing OS syscalls. Beyond that, C++ memory layouts can be carefully tuned for cache behavior; you don’t get that level of control in managed languages.

    Which of these factors has more influence is completely application-specific. Anyone making a blanket statement that “C++ is faster in general than Java” or “Java is faster in general than C++” is an idiot. Averages don’t matter. Performance on YOUR application matters.


    And here is my proof, that gcc is precomputing the answer.

    On this code:

    #include <stdio.h>
    #include <windows.h>
    
    unsigned long long s(unsigned long long n)
    {
        unsigned long long s = 0;
    
        for (unsigned long long i = 0; i < n; i++)
            s += i;
    
        return s;
    }
    
    int main( int argc, char** argv )
    {
        LARGE_INTEGER freq, start, end;
        QueryPerformanceFrequency(&freq);
        QueryPerformanceCounter(&start);
    
        printf("%llu\n", s(1000000000));
    
        QueryPerformanceCounter(&end);
        double d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;
    
        printf("Delta: %f\n", d);
    
        QueryPerformanceCounter(&start);
    
        printf("%llu\n", s(atol(argv[1])));
    
        QueryPerformanceCounter(&end);
        d = (double) (end.QuadPart - start.QuadPart) / freq.QuadPart * 1000.0;
    
        printf("Delta: %f\n", d);
        return 0;
    }
    

    With gcc-4.3.4, using the command-line ./g++-4 -omasoud-gcc.exe -O3 masoud.cpp:

    bash-3.2# ./masoud-gcc 1000000000
    499999999500000000
    Delta: 0.845755
    499999999500000000
    Delta: 1114.105866
    

    By comparison, MSVC++ 16.00.40219.01 for x64 (2010 SP1), command-line cl /Ox masoud.cpp:

    > masoud 1000000000
    499999999500000000
    Delta: 229.684364
    499999999500000000
    Delta: 354.275606
    

    VC++ isn’t precomputing the answer, but 64-bit code does execute the loop more than three times faster. This is the speed that Java ought to approach.


    More fun facts: gcc precomputes the answer faster than the code it generates to calculate it out. Compile time for gcc:

    real    0m0.886s
    user    0m0.248s
    sys     0m0.185s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perhaps a simple to be answered question, but I did not find a satisfying
What I'm trying to do seems very simple, but I did not yet find
Ok I know this is simple, in fact damn simple, I did this in
I have 2 classes: FirstDeep.cs SecondDeep.cs I did simple code for example: class FirstDeep
I was playing around with recursion and did this simple function. I was assuming
Did you ever have the following situation: you need to store information, but a
Last night I did a load test on a site. I found that one
so I know there are somewhat similar questions on here, but I haven't been
I'm running MinGW on windows XP SP3. I wrote a simple program in C++
I am using iBatis.NET in a very simple test project (VS 2008). When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.