Thread: Java is 1000 times faster than c++ on my computer???

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Seattle
    Posts
    6

    Java is 1000 times faster than c++ on my computer???

    Ok, this is driving me batty. I recently had an assignment to implement Floyds algorithm and Dijkstras on a graph with 5403 vertices. (in java) my program was running impossibly slow so I started troubleshooting. As part of that troubleshooting I wrote a 3 level nested for loop in Java, and the same set of loops in C++. The Java version is running 1000 times faster than the C++ version. I just KNOW Java is not 1000 times faster than C++ Can anybody suggest what might be going on here? Here's the code
    Code:
    In C++:
    
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	unsigned start = time(0);
    	cout << "start is " << start << endl;
    	int count=0;
        for (int i=0;i<5404;i++)
        {
        	cout << "i = " << i << endl;
        	for (int j=0;j<5404;j++)
        	{
            	for (int k=0;k<5404;k++)
            	{
            		count ++;
            	}
            }
    	}
    	unsigned finish = time(0);
    	unsigned total = finish-start;
    	cout << "n^3 took: "<< total << "seconds" <<  endl;
        return 0;
    }// end main
    
    Output: 
    	.
    	.
    	.
    i = 5400
    
    i = 5401
    
    i = 5402
    
    i = 5403
    
    finish is 1243916727
    
    n^3 took: 323seconds
    
    
    Java:
    public class Main 
    {
    	public static void main(String[] args)
    	{
    		int count=0;
    		long start = System.currentTimeMillis();
    		for(int k=0;k<5404;k++)
    		{
    			System.out.println( "k= " + k);
    			for (int i=0;i<5404;i++)
    			{
    				for(int j=0;j<5404;j++)
    				{
    					count++;
    				}
    			}
    		}
    		long finish=System.currentTimeMillis();
    		long totalTime=(finish-start);
    		System.out.println("n^3 took " + totalTime / 1000.0 + " seconds");
    		
    	}	
    } // end main
    
    Output:
    	.
    	.
    	.
    k= 5400
    
    k= 5401
    
    k= 5402
    
    k= 5403
    
    n^3 took 0.332 seconds
    Just to flesh out the details. My computer is an amd dual core 2.7Ghz with 4Gb Ram running Ubuntu 9.10.

    Java is
    java version "1.6.0_0"
    OpenJDK Runtime Environment (IcedTea6 1.4.1) (6b14-1.4.1-0ubuntu7)
    OpenJDK 64-Bit Server VM (build 14.0-b08, mixed mode)

    C compiler is:
    g++ (Ubuntu 4.3.3-5ubuntu4) 4.3.3
    Copyright (C) 2008 Free Software Foundation, Inc.

    commands
    java Main
    g++ main.cpp
    Last edited by darin722; 06-01-2009 at 10:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  2. Computer will not boot.
    By RealityFusion in forum Tech Board
    Replies: 25
    Last Post: 09-10-2004, 04:05 PM
  3. Regarding Undergraduate Computer Majors
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2003, 11:55 AM
  4. This is my last night on this computer.
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-12-2003, 01:33 AM
  5. Which distro should I run on my old computer?
    By joshdick in forum Tech Board
    Replies: 5
    Last Post: 04-09-2003, 01:37 AM