1

I'm writing a native android app which calculates the time elapsed in milliseconds using std::clock(). The sample code to calculate elapsed time is below:

elapsedTime = ((float)(std::clock() - startTime)/CLOCKS_PER_SEC) * 1000; 

However, it doesn't seem to match the regular clock (laptop's clock or regular watch) at all. For example, elapsedTime of 30 seconds comes out to be equal to 20 seconds of the regular watch. This works fine on previous versions of android (4.0 etc.) though. Any hints on why it's behaving differently on different versions of Android OS.

pree
  • 2,057
  • 6
  • 32
  • 50
  • 1
    Here it's explained std:clock may give rather big difference compared to wallclock (see example program output) ; http://en.cppreference.com/w/cpp/chrono/c/clock . – harism Jan 19 '15 at 21:51
  • I tried this code to calculate wallclock time, however, it was still running faster. Is there a way to calculate wall clock elapsed time? – pree Jan 19 '15 at 23:01

1 Answers1

1

Solution 1: (Reply by CTT): C++ obtaining milliseconds time on Linux -- clock() doesn't seem to work properly

Solution 2: (comment by Harism) Here it's explained std:clock may give rather big difference compared to wallclock (see example program output) ; en.cppreference.com/w/cpp/chrono/c/clock

Community
  • 1
  • 1
pree
  • 2,057
  • 6
  • 32
  • 50