1

What i do:

1.I called a c++ method from java by generating wrapper using the swig intermediate file.

2.I used clock() time inside c++ code, to calculate the total time taken in the method writtern in c++(Differnce from start to end) .

       clock_t start=clock();
       clock_t end=clock();
       double elapsed_secs = double(end - start) / (CLOCKS_PER_SEC)*1000;

3.In the java, i used System.CurrentTimeMillis() to calculate the time taken to call the method.

What i have to know:

When calculating the both difference in milliseconds,it was found that the time difference taken inside the c++ is more than the time difference while calling the method in java.

1.How the time taken while calling the method from java is less than the toatal time taken inside the method?

2.Is there any difference between the clock() time and the System.CurrentTimeMillis()?

Cibin William
  • 303
  • 1
  • 3
  • 17

1 Answers1

2

Please note I'm not a Java programmer.

1) System.currentTimeMillis isn't comparable to clock

Depending on implementation (see 2), clock will return (roughly) how long your program has been executing. System.currentTimeMillis returns UNIX time in milliseconds, which isn't quite correct compared to clock. It's also worth mentioning currentTimeMillis is also platform dependant.

On the Java side, you should be using System.nanoTime(), see How do I measure time elapsed in Java?

2) clock is defined differently on different platforms.

Put simply, whether clock returns Wall Time or CPU Time is dependant on the platform you're on.

To match System.nanoTime() to measure CPU time, use std::clock with std::chrono::high_resolution_clock. You can see a working example here: http://en.cppreference.com/w/cpp/chrono/c/clock

Community
  • 1
  • 1
058 094 041
  • 495
  • 3
  • 6