+ 2

How can I measure/compare the speed of my java/c++ program? (The two program do the same thing.)

Java vs C++ speed

13th Jan 2017, 2:31 AM
Herczeg DĂĄniel
Herczeg DĂĄniel - avatar
3 Answers
+ 5
Under linux, you don't need to write a timer in yours programs, as you can use 'time <command>' ( where <command> is the name of yours c++/java codes to test )... Probably Macs/osx have it too.
13th Jan 2017, 7:04 AM
visph
visph - avatar
+ 3
For JAVA get the time at the beginning and the end of the program. the difference will be the amount of milliseconds the program ran //output the time in milliseconds at the start System.out.println( "Start: " + System.currentTimeMillis()); ...your codegoes here... //output the time in milliseconds at the end System.out.println( "End" + System.currentTimeMillis()); check the output of your program and calculate the difference between start time and end time. Or you can save the start time and the end time in two long variables and output their difference in your last line of code. But this options would add additional instructions that according to the system you are using could increase the runtime of the program( I.e. if you have to write/access the values of start and end time from an sd card in an MCU circuit, the time needed to access the variables would increase the runtime by few milliseconds. On a modern PC this issue is unremarkable.
13th Jan 2017, 3:29 AM
seamiki
seamiki - avatar
+ 3
For C++ #include<iostream.h> #include <Time.h> // to use the clock() function using namespace std; int main(){ //output start time cout<<"Start: "<<clock(); ...your program goes here... //output end time cout<<"End: "<<clock(); return 0; Same remarks as for my previous java answer but if you decide to use variables, convert the time returned by clock() to float for a more accurate result.
13th Jan 2017, 3:43 AM
seamiki
seamiki - avatar