public
Description: Good code.
Homepage: http://www.ralree.com
Clone URL: git://github.com/hank/life.git
life / code / c / common / test.c
100644 93 lines (76 sloc) 1.95 kb
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "ekg_sys.h"
 
int main(int argc, char ** argv) {
  char * buf, * buf2;
  uint32_t buflen, failure = 0, i;
  unsigned int x,y;
  unsigned int speed[3];
 
  if(argc < 2) {
    // No input num, just use 1000
    buflen = 1000;
  } else {
    buflen = atoi(argv[1]);
    if(buflen == 0) return 0;
  }
 
  if(!(buf = malloc(buflen))) {
    printf("Couldn't allocate memory.\n");
    return 0;
  }
  printf("Allocated %u bytes\n", buflen);
 
  if(!(buf2 = malloc(buflen))) {
    printf("Couldn't allocate memory.\n");
    return 0;
  }
  printf("Allocated %u bytes\n", buflen);
 
  printf("Filling %u bytes with junk\n", buflen);
  for(i=0; i<buflen; i++) {
    buf[i] = buf2[i] = 1;
  }
 
  // TEST OF MEMCMP VS. FMEMCMP
  printf("***************** FMEMCMP *****************\n");
 
  printf("Running system memcmp: ");
  GET_CYCLES(x);
  if(memcmp(buf, buf2, buflen) != 0) failure = 1;
  GET_CYCLES(y);
  printf("time=%u, ", y-x);
  speed[0] = y-x;
 
  if(!failure) {
    printf("Match!\n");
  } else {
    printf("No Match!\n");
  }
  failure = 0;
 
  printf("Running forloop memcmp: ");
  GET_CYCLES(x);
  for(i=0; i<buflen; i++) {
    if(buf[i] != buf2[i]) failure = 1;
  }
  GET_CYCLES(y);
  printf("time=%u, ", y-x);
  speed[1] = y-x;
 
  if(!failure) {
    printf("Match!\n");
  } else {
    printf("No Match!\n");
  }
  failure = 0;
 
  printf("Running fmemcmp: ");
  GET_CYCLES(x);
  if((fmemcmp(buf, buf2, buflen)) != 0) failure = 1;
  GET_CYCLES(y);
  printf("time=%u, ", y-x);
  speed[2] = y-x;
 
  if(!failure) {
    printf("Match!\n");
  } else {
    printf("No Match! (%d)\n", failure);
  }
  failure = 0;
 
 
  // Summarize
  printf(":: fmemcmp is %g times faster than system memcmp.\n", ((float)speed[0])/speed[2]);
  printf(":: fmemcmp is %g times faster than forloop memcmp.\n", ((float)speed[1])/speed[2]);
 
  printf("***************** FMEMCMP *****************\n");
 
  return 1;
}