Question:
Java C++ help, im comparing the execution times of both languages but c++ took a lot of time w/ for loop.HELP?
Timmy Lacoste
2011-12-07 17:27:58 UTC
I am comparing C++ and java, java using eclipse, c++ using bloodshed dev C++
Java took about 16 seconds to run the for loop, but c++ took over an hour.
i was wondering if i did something wrong, or if i should switch the compiler, if so please reccomend a good free one.
My java code is as follows:
//import java.util.*;
public class Time
{
public Time(){}
public static void main(String args[])
{
long startTime = System.currentTimeMillis();
Time ext=new Time();
ext.callMethod();
long endTime = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"+ (endTime-startTime));
}
public void callMethod(){
System.out.println("Calling method");

for(int i=1;i<=1000000;i++)
{

System.out.println("Value of counter is "+i);
}

if you have suggestion for the code please comment.
for c++:
#include
#include
#include
using namespace std;
int main()
{
clock_t start, end;

start = clock();
int i;
for(i=0; i<1000000+1; i+=1){
system("Cls");
cout<< i << endl;}

end = clock();

cout << "Time required for execution: "
<< (double)(end-start)/CLOCKS_PER_SEC
<< " seconds." << "\n\n";
Sleep(10000);
return 0;

}
please give suggestions. Thank You!!!
Seven answers:
green meklar
2011-12-07 18:11:37 UTC
I suspect the problem with the C++ version is here:



system("Cls");



System calls are extremely expensive. The computer has to do a whole lot more stuff when you make a system call. To put into perspective, when Java calls System.out.println(), it's like writing a message on a slip of paper and handing it to your friend sitting next to you, and when C++ calls system("Cls"), it's like carving a message onto a giant block of marble and shipping it by air to your friend in South Africa. The latter just takes way more time and resources.



In any case, printing itself is already a relatively large resource hog. I suspect that if you remove all the commands from inside both for loops, and just measure the time it takes the processor to spin that much, you'll find that both cases will run much faster (probably in less than 10 milliseconds, although it depends on your system).
modulo_function
2011-12-07 17:39:36 UTC
Why are your for loops so different?



In the Java you use:

for(int i=1;i<=1000000;i++)





and in the C++ you use:

for(i=0; i<1000000+1; i+=1){



The C++ has more operations involved.

Furthermore, why are you calling

system("Cls");

in your C++ loop? This adds more time! How much? I don't know but it's certainly not zero.



It also depends on how you executed the codes. If you did them within the IDEs then there's significant overhead. To compare the execution of 2 codes you should execute the class file for Java and the exe file for C++.



You have a great future in advertising. Those are very different codes and yet you want to compare results. You comparison is deeply flawed.
Cubbi
2011-12-07 18:14:17 UTC
Replace



     system("Cls");

     cout<< i << endl;



with



     cout << "Value of counter is " << i << '\n';



to make it a fair comparison. (and no, "i<=1000000;i++" and "i<1000000+1; i+=1" are identical from compiler's point of view, that is not the source of any difference)



EDIT: apparently System.out.println() flushes the output buffer every time, so endl is in fact more appropriate than '\n'. But, in any case, what you're comparing is the speed of the screen output subsystem, which is hardly a representative comparison of the languages (most of the time is spent in the OS, not in your program)



On my home computer, your Java program executes in 1.40 seconds, and your C++ program with loop body " cout << "Value of counter is " << i << endl;" executes in 0.23 seconds (replacing endl with '\n' drops execution time to 0.18 seconds)
2016-10-18 03:38:57 UTC
before everything thank you Mr.TJ on your kindness.i easily get excitement from it.I edited and deleted that section because of the fact I felt i became into attempting to get particular interest from different clientele and that i felt undesirable approximately it later.That became into the reason.despite thank you lots : ) the respond - confident,he would be a brilliant advance for particular.I nonetheless remeber how he performed interior the WC.He became into dazzling and that i might fairly choose to make certain him play even even with the undeniable fact that he performs for the contest as he's a great participant and he's a handle to video demonstrate to all the human beings not in basic terms his countrymen and that i'm waiting to make certain him play and that i can make certain him for authentic additionally as i visit video demonstrate lots of the T20 WC suits stay on the stadium and all the different cricketers to boot.waiting forward to that yet on the 2d i'm basically feeling disenchanted. i think of Rohit Sharma would be interior the 15 as i think of the BCCI have relied on him lots.I easily have seen him play enormous T20 knocks and has been a tournament winner at cases.approximately Yusuf Pathan i'm not so particular.He failed interior the IPL additionally for KKR so i don't think of he would be lined.
2011-12-07 17:35:46 UTC
im not that much into c++ but i think the time consuming thing is system("Cls"); because it always runs a system command, which honestly is very time consuming. In java code you use system.out, which also is time consuming but not that much. try cout << "cls" << endl; and it might be much faster..
Fredmaster
2011-12-07 17:38:19 UTC
In addition to what's been said already, using i+=1 instead of i++ for the C++ version may be slowing it down (I don't know how good the compiler for dev C++ is at optimizing).
ty2up
2011-12-07 17:35:13 UTC
Where is your clock() function at?

Plus what are these.../* clock_t start, end;*/

functions maybe? if so need to be like this...

int clock_t(int Start,int end)

{

//Code here

return end;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...