Need clock_gettime() help

OK. So here is the task at hand. I need this function to be able to return the milliseconds that it takes to time how long something takes to run. Assume that the rest of the program works to allow me to return (because it does). I just need to know what I am doing wrong due to the fact that it only outputs either the number "2" or a value similar to "3.163e+06". I just need a solid number. So, here is the method:

1
2
3
4
5
6
7
8
9
10
 double
  Timer::getElapsedMs() const
  {
    clock_t secDifference = m_stop.tv_sec - m_start.tv_sec;
    clock_t nsecDifference = m_stop.tv_nsec - m_start.tv_nsec;
	
    double totalMsTime  = (static_cast<double> (secDifference) / MILLISECONDS_PER_SECOND) + (static_cast<double> (nsecDifference) * MILLISECONDS_PER_SECOND);
											     
    return totalMsTime;
  }


It takes the start and stop values from the both the start and stop member variables and subtracts them to get the difference. The issue is that it, again, just outputs one of the two aforementioned results...

Any help would be great. Thanks!
Why do you store a difference of two time_ts and a difference of two long ints in clock_t variables?? It isn't the problem here, I am just trying to understand the thought process.

As for the problems, to get the milliseconds, you need to multiply (not divide) seconds by a thousand and you need to divide (not multiply) nanoseconds by a million (not a thousand!)
Topic archived. No new replies allowed.