Sha256: c5d5f0097924e44b2fefb589749fa5f8b8d30ef081b7f2250b2ff3f05158a6f3

Contents?: true

Size: 668 Bytes

Versions: 5

Compression:

Stored size: 668 Bytes

Contents

#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include "timer.h"

static struct timeval before, after;

void start_timer() {
  before.tv_sec = 0;
  before.tv_usec = 0;
  after.tv_sec = 0;
  after.tv_usec = 0;

  gettimeofday(&before, NULL);
}
long long stop_timer() {
  gettimeofday(&after, NULL);

  // Two subtractions can be done here without decreasing stability because
  // tv_sec and tv_usec are not floating point format. Doing the subtractions
  // separately before the multiplication and addition reduces the
  // possibility of overflow.
  return (
      (after.tv_sec - before.tv_sec) * 1e6 +
      (after.tv_usec - before.tv_usec)
  );
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
c_project-0.2.3 templates/c/vendor/timer/src/timer.c
c_project-0.2.2 templates/c/vendor/timer/src/timer.c
c_project-0.2.1 templates/c/vendor/timer/src/timer.c
c_project-0.2.0 templates/c/vendor/timer/src/timer.c
c_project-0.1.0 templates/c/vendor/timer/src/timer.c