"Fossies" - the Fresh Open Source Software Archive 
Member "xdelta3-3.0.11/examples/compare_test.c" (24 Mar 2015, 2430 Bytes) of package /linux/misc/xdelta3-3.0.11.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include "xdelta3.h"
8
9 #define NUM (1<<20)
10 #define ITERS 100
11
12 /* From wikipedia on RDTSC */
13 inline uint64_t rdtsc() {
14 uint32_t lo, hi;
15 asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
16 return (uint64_t)hi << 32 | lo;
17 }
18
19 typedef int (*test_func)(const char *s1, const char *s2, int n);
20
21 void run_test(const char *buf1, const char *buf2,
22 const char *name, test_func func) {
23 uint64_t start, end;
24 uint64_t accum = 0;
25 int i, x;
26
27 for (i = 0; i < ITERS; i++) {
28 start = rdtsc();
29 x = func(buf1, buf2, NUM);
30 end = rdtsc();
31 accum += end - start;
32 assert(x == NUM - 1);
33 }
34
35 accum /= ITERS;
36
37 printf("%s : %qu cycles\n", name, accum);
38 }
39
40 /* Build w/ -fno-builtin for this to be fast, this assumes that there
41 * is a difference at s1[n-1] */
42 int memcmp_fake(const char *s1, const char *s2, int n) {
43 int x = memcmp(s1, s2, n);
44 return x < 0 ? n - 1 : n + 1;
45 }
46
47 #define UNALIGNED_OK 1
48 static inline int
49 test2(const char *s1c, const char *s2c, int n)
50 {
51 int i = 0;
52 #if UNALIGNED_OK
53 int nint = n / sizeof(int);
54
55 if (nint >> 3)
56 {
57 int j = 0;
58 const int *s1 = (const int*)s1c;
59 const int *s2 = (const int*)s2c;
60 int nint_8 = nint - 8;
61
62 while (i <= nint_8 &&
63 s1[i++] == s2[j++] &&
64 s1[i++] == s2[j++] &&
65 s1[i++] == s2[j++] &&
66 s1[i++] == s2[j++] &&
67 s1[i++] == s2[j++] &&
68 s1[i++] == s2[j++] &&
69 s1[i++] == s2[j++] &&
70 s1[i++] == s2[j++]) { }
71
72 i = (i - 1) * sizeof(int);
73 }
74 #endif
75
76 while (i < n && s1c[i] == s2c[i])
77 {
78 i++;
79 }
80 return i;
81 }
82
83 static inline int
84 test1(const char *s1c, const char *s2c, int n) {
85 int i = 0;
86 while (i < n && s1c[i] == s2c[i])
87 {
88 i++;
89 }
90 return i;
91 }
92
93 int main(/*int argc, char **argv*/) {
94 char *buf1 = malloc(NUM+1);
95 char *buf2 = malloc(NUM+1);
96 int i;
97
98 for (i = 0; i < NUM; i++) {
99 buf1[i] = buf2[i] = rand();
100 }
101
102 buf2[NUM-1]++;
103
104 printf ("ALIGNED\n");
105
106 run_test(buf1, buf2, "memcmp", &memcmp_fake);
107 run_test(buf1, buf2, "test1", &test1);
108 run_test(buf1, buf2, "test2", &test2);
109
110 for (i = 0; i < NUM; i++) {
111 buf1[i] = buf2[i+1] = rand();
112 }
113
114 buf2[NUM]++;
115
116 printf ("UNALIGNED\n");
117
118 run_test(buf1, buf2+1, "memcmp", &memcmp_fake);
119 run_test(buf1, buf2+1, "test1", &test1);
120 run_test(buf1, buf2+1, "test2", &test2);
121
122 return 0;
123 }