summaryrefslogtreecommitdiff
path: root/stringbench.c
blob: 7dcc9fd3f15440a4e7b213fee52018570c147a55 (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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/time.h>

#define START gettimeofday(&tv1, 0);
#define END(x) gettimeofday(&tv2, 0); t1 = ((double)tv1.tv_sec)+((double)tv1.tv_usec)/1000000.0; t2 = ((double)tv2.tv_sec)+((double)tv2.tv_usec)/1000000.0; printf("%s: %f seconds\n", x, t2-t1)

int main(int argc __unused, char **argv __unused) {
	struct timeval tv1, tv2;
	double t1, t2;
	char * const s = (char * const) malloc(16);
	char * const l = (char * const) malloc(15001);
	char * const tmp = (char * const) malloc(15001);

	for(int i=0; i<15; i++)
		s[i]='a';
	s[15]=0;

	START;
	for(int i=0; i<10000000; i++) {
		memset(l, 'a', 15000);
	}
	END("10000000 * memset 15000*'a'");

	l[15000]=0;

	START;
	for(int i=0; i<500000000; i++)
		if(strlen(s) != 15)
			fprintf(stderr, "strlen broken\n");
	END("500000000 * strlen(15*a)");

	START;
	for(int i=0; i<1000000; i++)
		if(strlen(l) != 15000)
			fprintf(stderr, "strlen broken\n");
	END("1000000 * strlen(15000*a)");

	START;
	for(int i=0; i<10000000; i++)
		if(memchr(l, 'b', 15000))
			fprintf(stderr, "bogus memchr\n");
	END("10000000 * memchr not finding anything in 15000 chars");

	START;
	for(int i=0; i<10000000; i++)
		if(strchr(l, 'b'))
			fprintf(stderr, "bogus strchr\n");
	END("10000000 * strchr not finding anything in 15000 chars");

	l[5000]='b';
	START;
	for(int i=0; i<10000000; i++) {
		char *b=memchr(l, 'b', 15000);
		if(!b || *b != 'b')
			fprintf(stderr, "bogus memchr II\n");
	}
	END("10000000 * memchr finding match at 5000");

	START;
	for(int i=0; i<10000000; i++) {
		char *b=strchr(l, 'b');
		if(!b || *b != 'b')
			fprintf(stderr, "bogus strchr II\n");
	}
	END("10000000 * strchr finding match at 5000");

	START;
	for(int i=0; i<10000000; i++) {
		memcpy(tmp, l, 15000);
	}
	END("10000000 * memcpy of 15000 bytes");

	START;
	for(int i=0; i<900000000; i++) {
		memcpy(tmp, s, 15);
	}
	END("900000000 * memcpy of 15 bytes");

	START;
	for(int i=0; i<1000000; i++) {
		strcpy(tmp, l);
	}
	END("1000000 * strcpy of 15000 bytes");

	START;
	for(int i=0; i<100000000; i++) {
		strcpy(tmp, s);
	}
	END("100000000 * strcpy of 15 bytes");

	START;
	for(int i=0; i<15000; i++) {
		memset(tmp, 0, 15000);
		for(int j=0; j<15000; j++) {
			strcat(tmp, "a");
		}
	}
	END("15000 * 15000 * strcat + 1 byte");

	memset(l, 0, 15000);
	memset(l, 'a', 1000);
	START;
	for(int i=0; i<15000; i++) {
		memset(tmp, 0, 15000);
		for(int j=0; j<15; j++) {
			strcat(tmp, l);
		}
	}
	END("15000 * 15 * strcat + 1000 bytes");

	return 0;
}