Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <benchmarks.h>
10 :
11 : #include <ctype.h>
12 :
13 0 : int owncmp (const char * str1, const char * str2)
14 : {
15 0 : while (*str1 && (*str1 == *str2))
16 : {
17 0 : str1++;
18 0 : str2++;
19 : }
20 0 : return *(const unsigned char *) str1 - *(const unsigned char *) str2;
21 : }
22 :
23 : // warning: these are not correct implementations, but just to get an
24 : // impression about performance
25 :
26 0 : int slacmp (const char * str1, const char * str2)
27 : {
28 0 : while (*str1 && (*str1 == *str2))
29 : {
30 0 : str1++;
31 0 : str2++;
32 : }
33 :
34 0 : if (*str1 == '/')
35 : {
36 : return 1;
37 : }
38 0 : else if (*str2 == '/')
39 : {
40 : return -1;
41 : }
42 0 : return *(const unsigned char *) str1 - *(const unsigned char *) str2;
43 : // found different char
44 : }
45 :
46 0 : int natcmp (const char * str1, const char * str2)
47 : {
48 0 : int count_num = -1;
49 0 : while (*str1 && (*str1 == *str2))
50 : {
51 0 : str1++;
52 0 : str2++;
53 0 : if (*str1 == '#')
54 : {
55 : count_num = 0;
56 : }
57 0 : else if (count_num >= 0 && !isdigit (*str1))
58 : {
59 0 : ++count_num;
60 : }
61 : else
62 : {
63 : count_num = -1;
64 : }
65 : }
66 :
67 0 : if (count_num > 0)
68 : {
69 : return 12;
70 : }
71 :
72 0 : if (*str1 == '/')
73 : {
74 : return 1;
75 : }
76 0 : else if (*str2 == '/')
77 : {
78 : return -1;
79 : }
80 0 : return *(const unsigned char *) str1 - *(const unsigned char *) str2;
81 : }
82 :
83 :
84 0 : int main (void)
85 : {
86 0 : long long nrIterations = 100000000;
87 0 : const char str1[] = "some string to be compared with/some\\/more with a long common part, and only a bit different";
88 0 : char * str2 = elektraMalloc (sizeof (str1));
89 0 : strcat (str2, str1);
90 0 : str2[sizeof (str1) - 5] = 'X';
91 :
92 0 : int res = 0;
93 :
94 0 : timeInit ();
95 0 : for (int i = 0; i < nrIterations; ++i)
96 : {
97 0 : res ^= strcmp (str1, str2);
98 : }
99 0 : timePrint ("strcmp");
100 0 : for (int i = 0; i < nrIterations; ++i)
101 : {
102 0 : res ^= memcmp (str1, str2, sizeof (str1));
103 : }
104 0 : timePrint ("memcmp");
105 0 : for (int i = 0; i < nrIterations; ++i)
106 : {
107 0 : res ^= owncmp (str1, str2);
108 : }
109 0 : timePrint ("owncmp");
110 0 : for (int i = 0; i < nrIterations; ++i)
111 : {
112 0 : res ^= slacmp (str1, str2);
113 : }
114 0 : timePrint ("slacmp");
115 0 : for (int i = 0; i < nrIterations; ++i)
116 : {
117 0 : res ^= natcmp (str1, str2);
118 : }
119 0 : timePrint ("natcmp");
120 :
121 0 : printf ("%d\n", res);
122 : }
|