main.c 3.4 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>

#define POINTER_SHIFT(ptr, s) ((void *)(((char *)ptr) + (s)))
#define POINTER_DISTANCE(pa, pb) ((char *)(pb) - (char *)(pa))

#define tPutA(buf, val)              \
  ({                                 \
    memcpy(buf, &val, sizeof(val));  \
    POINTER_SHIFT(buf, sizeof(val)); \
  })

#define tPutB(buf, val)                         \
  ({                                            \
H
more  
Hongze Cheng 已提交
17 18 19 20
    ((uint8_t *)buf)[7] = ((val) >> 56) & 0xff; \
    ((uint8_t *)buf)[6] = ((val) >> 48) & 0xff; \
    ((uint8_t *)buf)[5] = ((val) >> 40) & 0xff; \
    ((uint8_t *)buf)[4] = ((val) >> 32) & 0xff; \
H
Hongze Cheng 已提交
21 22 23 24 25 26 27
    ((uint8_t *)buf)[3] = ((val) >> 24) & 0xff; \
    ((uint8_t *)buf)[2] = ((val) >> 16) & 0xff; \
    ((uint8_t *)buf)[1] = ((val) >> 8) & 0xff;  \
    ((uint8_t *)buf)[0] = (val)&0xff;           \
    POINTER_SHIFT(buf, sizeof(val));            \
  })

H
more  
Hongze Cheng 已提交
28 29 30 31 32 33 34
#define tPutC(buf, val)                \
  ({                                   \
    if (buf) {                         \
      ((uint64_t *)buf)[0] = (val);    \
      POINTER_SHIFT(buf, sizeof(val)); \
    }                                  \
    NULL;                                 \
H
Hongze Cheng 已提交
35 36
  })

H
more  
Hongze Cheng 已提交
37 38 39 40 41 42 43 44 45 46
#define tPutD(buf, val)                        \
  ({                                           \
    uint64_t tmp = val;                        \
    for (size_t i = 0; i < sizeof(val); i++) { \
      ((uint8_t *)buf)[i] = tmp & 0xff;        \
      tmp >>= 8;                               \
    }                                          \
    POINTER_SHIFT(buf, sizeof(val));           \
  })

H
more  
Hongze Cheng 已提交
47 48 49 50 51 52 53 54
static inline void tPutE(void **buf, uint64_t val) {
  if (buf) {
    ((uint64_t *)(*buf))[0] = val;
    *buf = POINTER_SHIFT(*buf, sizeof(val));
  }
}

typedef enum { A, B, C, D, E } T;
H
Hongze Cheng 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

static void func(T t) {
  uint64_t val = 198;
  char     buf[1024];
  void *   pBuf = buf;

  switch (t) {
    case A:
      for (size_t i = 0; i < 10 * 1024l * 1024l * 1024l; i++) {
        pBuf = tPutA(pBuf, val);
        if (POINTER_DISTANCE(buf, pBuf) == 1024) {
          pBuf = buf;
        }
      }
      break;
    case B:
      for (size_t i = 0; i < 10 * 1024l * 1024l * 1024l; i++) {
        pBuf = tPutB(pBuf, val);
        if (POINTER_DISTANCE(buf, pBuf) == 1024) {
          pBuf = buf;
        }
      }
      break;
    case C:
      for (size_t i = 0; i < 10 * 1024l * 1024l * 1024l; i++) {
        pBuf = tPutC(pBuf, val);
        if (POINTER_DISTANCE(buf, pBuf) == 1024) {
          pBuf = buf;
        }
      }
      break;
H
more  
Hongze Cheng 已提交
86 87 88 89 90 91 92 93
    case D:
      for (size_t i = 0; i < 10 * 1024l * 1024l * 1024l; i++) {
        pBuf = tPutD(pBuf, val);
        if (POINTER_DISTANCE(buf, pBuf) == 1024) {
          pBuf = buf;
        }
      }
      break;
H
more  
Hongze Cheng 已提交
94 95 96 97 98 99 100 101
    case E:
      for (size_t i = 0; i < 10 * 1024l * 1024l * 1024l; i++) {
        tPutE(&pBuf, val);
        if (POINTER_DISTANCE(buf, pBuf) == 1024) {
          pBuf = buf;
        }
      }
      break;
H
Hongze Cheng 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    default:
      break;
  }
}

static uint64_t now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);

  return tv.tv_sec * 1000000 + tv.tv_usec;
}

int main(int argc, char const *argv[]) {
  uint64_t t1 = now();
  func(A);
  uint64_t t2 = now();
  printf("A: %ld\n", t2 - t1);
  func(B);
  uint64_t t3 = now();
  printf("B: %ld\n", t3 - t2);
  func(C);
  uint64_t t4 = now();
  printf("C: %ld\n", t4 - t3);
H
more  
Hongze Cheng 已提交
126 127 128
  func(D);
  uint64_t t5 = now();
  printf("D: %ld\n", t5 - t4);
H
more  
Hongze Cheng 已提交
129 130 131
  func(E);
  uint64_t t6 = now();
  printf("E: %ld\n", t6 - t5);
H
Hongze Cheng 已提交
132 133
  return 0;
}