compare_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* xdelta3 - delta compression tools and library
  2. Copyright 2016 Joshua MacDonald
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include "xdelta3.h"
  19. #define NUM (1<<20)
  20. #define ITERS 100
  21. /* From wikipedia on RDTSC */
  22. inline uint64_t rdtsc() {
  23. uint32_t lo, hi;
  24. asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
  25. return (uint64_t)hi << 32 | lo;
  26. }
  27. typedef int (*test_func)(const char *s1, const char *s2, int n);
  28. void run_test(const char *buf1, const char *buf2,
  29. const char *name, test_func func) {
  30. uint64_t start, end;
  31. uint64_t accum = 0;
  32. int i, x;
  33. for (i = 0; i < ITERS; i++) {
  34. start = rdtsc();
  35. x = func(buf1, buf2, NUM);
  36. end = rdtsc();
  37. accum += end - start;
  38. assert(x == NUM - 1);
  39. }
  40. accum /= ITERS;
  41. printf("%s : %qu cycles\n", name, accum);
  42. }
  43. /* Build w/ -fno-builtin for this to be fast, this assumes that there
  44. * is a difference at s1[n-1] */
  45. int memcmp_fake(const char *s1, const char *s2, int n) {
  46. int x = memcmp(s1, s2, n);
  47. return x < 0 ? n - 1 : n + 1;
  48. }
  49. #define UNALIGNED_OK 1
  50. static inline int
  51. test2(const char *s1c, const char *s2c, int n)
  52. {
  53. int i = 0;
  54. #if UNALIGNED_OK
  55. int nint = n / sizeof(int);
  56. if (nint >> 3)
  57. {
  58. int j = 0;
  59. const int *s1 = (const int*)s1c;
  60. const int *s2 = (const int*)s2c;
  61. int nint_8 = nint - 8;
  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. i = (i - 1) * sizeof(int);
  72. }
  73. #endif
  74. while (i < n && s1c[i] == s2c[i])
  75. {
  76. i++;
  77. }
  78. return i;
  79. }
  80. static inline int
  81. test1(const char *s1c, const char *s2c, int n) {
  82. int i = 0;
  83. while (i < n && s1c[i] == s2c[i])
  84. {
  85. i++;
  86. }
  87. return i;
  88. }
  89. int main(/*int argc, char **argv*/) {
  90. char *buf1 = malloc(NUM+1);
  91. char *buf2 = malloc(NUM+1);
  92. int i;
  93. for (i = 0; i < NUM; i++) {
  94. buf1[i] = buf2[i] = rand();
  95. }
  96. buf2[NUM-1]++;
  97. printf ("ALIGNED\n");
  98. run_test(buf1, buf2, "memcmp", &memcmp_fake);
  99. run_test(buf1, buf2, "test1", &test1);
  100. run_test(buf1, buf2, "test2", &test2);
  101. for (i = 0; i < NUM; i++) {
  102. buf1[i] = buf2[i+1] = rand();
  103. }
  104. buf2[NUM]++;
  105. printf ("UNALIGNED\n");
  106. run_test(buf1, buf2+1, "memcmp", &memcmp_fake);
  107. run_test(buf1, buf2+1, "test1", &test1);
  108. run_test(buf1, buf2+1, "test2", &test2);
  109. return 0;
  110. }