Q:

Measuring execution time - gettimeofday versus clock() versus chrono

I have a subroutine that should be executed once every milisecond. I wanted to check that indeed that's what's happening. But I get different execution times from different functions. I've been trying to understand the differences between these functions (there are several SO questions about the subject) but I cannot get my head around the results I got. Please forget the global variables etc. This is a legacy code, written in C, ported to C++, which I'm trying to improve, so is messy.
< header stuff>
std::chrono::high_resolution_clock::time_point tchrono;
int64_t tgettime;
float tclock;

void myfunction(){

<all kinds of calculations>

  using ms = std::chrono::duration<double, std::milli>;
  std::chrono::high_resolution_clock::time_point tmpchrono = std::chrono::high_resolution_clock::now();
  printf("chrono %f (ms): \n",std::chrono::duration_cast<ms>(tmpchrono-tchrono).count());
  tchrono = tmpchrono;

  struct timeval tv;
  gettimeofday (&tv, NULL);
  int64_t tmpgettime = (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
  printf("gettimeofday: %lld\n",tmpgettime-tgettime);
  tgettime = tmpgettime;

  float tmpclock = 1000.0f*((float)clock())/CLOCKS_PER_SEC;
  printf("clock %f (ms)\n",tmpclock-tclock);
  tclock = tmpclock;

  <more stuff>
}

and the output is:
chrono 0.998352 (ms): 
gettimeofday: 999
clock 0.544922 (ms)

Why the difference? I'd expect clock to be at least as large as the others, or not?

A:

std::chrono::high_resolution_clock::now() is not even working.

std::chrono::milliseconds represents the milliseconds as integers. When you convert to that representation, time representations of higher granularity are truncated to whole milliseconds. Then you assign it to a duration that has a double representation and seconds-ratio. Then you pass the duration object - instead of a double - to printf. All of those steps are wrong.
To get the milliseconds as a floating point, do this:
using ms = std::chrono::duration<double, std::milli>;
std::chrono::duration_cast<ms>(tmpchrono-tchrono).count();

clock() returns the processor time the process has used. That will depend on how much time the OS scheduler has given to your process. Unless the process is the only one on the system, this will be different from the passed wall clock time.
gettimeofday() returns the wall clock time.

What's the difference between using high_resolution_clock::now() and gettimeofday() ? 

Both measure the wall clock time. The internal representation of both is implementation defined. The granularity of both is implementation defined as well.
gettimeofday is part of the POSIX standard and therefore available in all operating systems that comply with that standard (POSIX.1-2001). gettimeofday is not monotonic, i.e. it's affected by things like setting the time (by ntpd or by adminstrator) and changes in daylight saving time.

high_resolution_clock represents the clock with the smallest tick period provided by the implementation. It may be an alias of std::chrono::system_clock or std::chrono::steady_clock, or a third, independent clock.

high_resolution_clock is part of the c++ standard library and therefore available in all compilers that comply with that standard (c++11). high_resolution_clock may or might not be monotonic. This can be tested with high_resolution_clock::is_steady

