summaryrefslogtreecommitdiffstats
path: root/runtime/probes/bench/time.c
blob: fb423276d5f5c00e444144d529d5f47316860082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>

typedef unsigned long long uint64;

struct timeval t1;

void start(struct timeval *tv)
{
  gettimeofday (tv, NULL);
}

uint64 time_delta(struct timeval *start, struct timeval *stop)
{
  uint64 secs, usecs;
  
  secs = stop->tv_sec - start->tv_sec;
  usecs = stop->tv_usec - start->tv_usec;
  if (usecs < 0) {
    secs--;
    usecs += 1000000;
  }
  return secs * 1000000 + usecs;
}

uint64 stop(struct timeval *begin)
{
  struct timeval end;
  gettimeofday (&end, NULL);
  return time_delta (begin, &end);
}


int main()
{
  int fd, i;
  char buf[1024];
  uint64 nsecs;

  system ("touch foo");
  fd = open ("foo", O_RDWR);

  start(&t1);
  for (i = 0; i < 1000000; i++) {
    if (read (fd, buf, 0) < 0)
      perror("read");
  }
  nsecs = stop(&t1) / 1000;

  printf("%lld ", nsecs);

  start(&t1);
  for (i = 0; i < 1000000; i++) {
    if (write (fd, buf, 0) < 0)
      perror("write");
  }
  nsecs = stop(&t1) / 1000;
  close (fd);

  printf("%lld\n", nsecs);

  return 0;
}