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
Just to flesh out the details. My computer is an amd dual core 2.7Ghz with 4Gb Ram running Ubuntu 9.10.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
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



LinkBack URL
About LinkBacks



CornedBee