This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
cmp.c
122 lines (108 loc) · 2.01 KB
/
cmp.c
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
115
116
117
118
119
120
121
122
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <benchmarks.h>
#include <ctype.h>
int owncmp (const char * str1, const char * str2)
{
while (*str1 && (*str1 == *str2))
{
str1++;
str2++;
}
return *(const unsigned char *) str1 - *(const unsigned char *) str2;
}
// warning: these are not correct implementations, but just to get an
// impression about performance
int slacmp (const char * str1, const char * str2)
{
while (*str1 && (*str1 == *str2))
{
str1++;
str2++;
}
if (*str1 == '/')
{
return 1;
}
else if (*str2 == '/')
{
return -1;
}
return *(const unsigned char *) str1 - *(const unsigned char *) str2;
// found different char
}
int natcmp (const char * str1, const char * str2)
{
int count_num = -1;
while (*str1 && (*str1 == *str2))
{
str1++;
str2++;
if (*str1 == '#')
{
count_num = 0;
}
else if (count_num >= 0 && !isdigit (*str1))
{
++count_num;
}
else
{
count_num = -1;
}
}
if (count_num > 0)
{
return 12;
}
if (*str1 == '/')
{
return 1;
}
else if (*str2 == '/')
{
return -1;
}
return *(const unsigned char *) str1 - *(const unsigned char *) str2;
}
int main (void)
{
long long nrIterations = 100000000;
const char str1[] = "some string to be compared with/some\\/more with a long common part, and only a bit different";
char * str2 = elektraMalloc (sizeof (str1));
strcat (str2, str1);
str2[sizeof (str1) - 5] = 'X';
int res = 0;
timeInit ();
for (int i = 0; i < nrIterations; ++i)
{
res ^= strcmp (str1, str2);
}
timePrint ("strcmp");
for (int i = 0; i < nrIterations; ++i)
{
res ^= memcmp (str1, str2, sizeof (str1));
}
timePrint ("memcmp");
for (int i = 0; i < nrIterations; ++i)
{
res ^= owncmp (str1, str2);
}
timePrint ("owncmp");
for (int i = 0; i < nrIterations; ++i)
{
res ^= slacmp (str1, str2);
}
timePrint ("slacmp");
for (int i = 0; i < nrIterations; ++i)
{
res ^= natcmp (str1, str2);
}
timePrint ("natcmp");
printf ("%d\n", res);
}