public
Description: Good code.
Homepage: http://www.ralree.com
Clone URL: git://github.com/hank/life.git
life / code / c / common / test.c
57f3b5f9 » ekgregg 2009-04-10 Yeah fmemcmp! A faster memc... 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include "ekg_sys.h"
5
6 int main(int argc, char ** argv) {
7 char * buf, * buf2;
8 uint32_t buflen, failure = 0, i;
9 unsigned int x,y;
10 unsigned int speed[3];
11
12 if(argc < 2) {
13 // No input num, just use 1000
14 buflen = 1000;
15 } else {
16 buflen = atoi(argv[1]);
17 if(buflen == 0) return 0;
18 }
19
20 if(!(buf = malloc(buflen))) {
21 printf("Couldn't allocate memory.\n");
22 return 0;
23 }
24 printf("Allocated %u bytes\n", buflen);
25
26 if(!(buf2 = malloc(buflen))) {
27 printf("Couldn't allocate memory.\n");
28 return 0;
29 }
30 printf("Allocated %u bytes\n", buflen);
31
32 printf("Filling %u bytes with junk\n", buflen);
33 for(i=0; i<buflen; i++) {
34 buf[i] = buf2[i] = 1;
35 }
36
37 // TEST OF MEMCMP VS. FMEMCMP
38 printf("***************** FMEMCMP *****************\n");
39
40 printf("Running system memcmp: ");
41 GET_CYCLES(x);
42 if(memcmp(buf, buf2, buflen) != 0) failure = 1;
43 GET_CYCLES(y);
44 printf("time=%u, ", y-x);
45 speed[0] = y-x;
46
47 if(!failure) {
48 printf("Match!\n");
49 } else {
50 printf("No Match!\n");
51 }
52 failure = 0;
53
54 printf("Running forloop memcmp: ");
55 GET_CYCLES(x);
56 for(i=0; i<buflen; i++) {
57 if(buf[i] != buf2[i]) failure = 1;
58 }
59 GET_CYCLES(y);
60 printf("time=%u, ", y-x);
61 speed[1] = y-x;
62
63 if(!failure) {
64 printf("Match!\n");
65 } else {
66 printf("No Match!\n");
67 }
68 failure = 0;
69
70 printf("Running fmemcmp: ");
71 GET_CYCLES(x);
72 if((fmemcmp(buf, buf2, buflen)) != 0) failure = 1;
73 GET_CYCLES(y);
74 printf("time=%u, ", y-x);
75 speed[2] = y-x;
76
77 if(!failure) {
78 printf("Match!\n");
79 } else {
80 printf("No Match! (%d)\n", failure);
81 }
82 failure = 0;
83
84
85 // Summarize
86 printf(":: fmemcmp is %g times faster than system memcmp.\n", ((float)speed[0])/speed[2]);
87 printf(":: fmemcmp is %g times faster than forloop memcmp.\n", ((float)speed[1])/speed[2]);
88
89 printf("***************** FMEMCMP *****************\n");
90
91 return 1;
92 }