aboutsummaryrefslogtreecommitdiff
path: root/tests/performance/strnstr.cpp
blob: 7c3b3ba7d734811813a0bd9f8d2b4435a43e8c1b (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <algorithm>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <random>
#include <string>
#include <tuple>
#include <vector>

char *ndpi_strnstr(const char *s, const char *find, size_t slen) {
  char c;
  size_t len;

  if ((c = *find++) != '\0') {
    len = strnlen(find, slen);
    do {
      char sc;

      do {
        if (slen-- < 1 || (sc = *s++) == '\0') return (NULL);
      } while (sc != c);
      if (len > slen) return (NULL);
    } while (strncmp(s, find, len) != 0);
    s--;
  }

  return ((char *)s);
}

char *ndpi_strnstr_opt(const char *haystack, const char *needle, size_t len) {
  if (!haystack || !needle || len == 0) {
    return NULL;
  }

  size_t needle_len = strlen(needle);
  size_t hs_real_len = strnlen(haystack, len);

  if (needle_len == 0) {
    return (char *)haystack;
  }

  if (needle_len > hs_real_len) {
    return NULL;
  }

  if (needle_len == 1) {
    return (char *)memchr(haystack, *needle, hs_real_len);
  }

  const char *current = haystack;
  const char *haystack_end = haystack + hs_real_len;

  while (current <= haystack_end - needle_len) {
    current = (const char *)memchr(current, *needle, haystack_end - current);

    if (!current) {
      return NULL;
    }

    if ((current + needle_len <= haystack_end) &&
        memcmp(current, needle, needle_len) == 0) {
      return (char *)current;
    }

    current++;
  }

  return NULL;
}

std::string random_string(size_t length, std::mt19937 &gen) {
  std::uniform_int_distribution<> dis(0, 255);
  std::string str(length, 0);
  for (size_t i = 0; i < length; i++) {
    str[i] = static_cast<char>(dis(gen));
  }
  return str;
}

double measure_time(const std::function<char *(const char *, const char *,
                                               size_t)> &strnstr_impl,
                    const std::string &haystack, const std::string &needle) {
  auto start = std::chrono::high_resolution_clock::now();

  volatile auto result =
      strnstr_impl(haystack.c_str(), needle.c_str(), haystack.size());
  auto end = std::chrono::high_resolution_clock::now();

  return std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)
      .count();
}

void warm_up(const std::function<char *(const char *, const char *, size_t)>
                 &strnstr_impl,
             const std::string &haystack, const std::string &needle,
             int iterations) {
  for (int i = 0; i < iterations; i++) {
    volatile auto result =
        strnstr_impl(haystack.c_str(), needle.c_str(), haystack.size());
  }
}

double average_without_extremes(const std::vector<double> &times) {
  if (times.size() < 5) {
    return std::accumulate(times.begin(), times.end(), 0.0) /
           static_cast<double>(times.size());
  }

  auto sorted_times = times;
  std::sort(sorted_times.begin(), sorted_times.end());
  sorted_times.erase(sorted_times.begin());
  sorted_times.pop_back();

  return std::accumulate(sorted_times.begin(), sorted_times.end(), 0.0) /
         sorted_times.size();
}

int main() {
  std::ios_base::sync_with_stdio(false);
  std::mt19937 gen(std::random_device{}());

  const std::vector<size_t> haystack_lengths = {
      128, 256,  368,  448,  512,  640,  704,  768,  832, 896,
      960, 1024, 1088, 1152, 1216, 1280, 1344, 1408, 1472};
  const std::vector<size_t> needle_lengths = {5,  10, 15, 20, 25, 30,
                                              35, 40, 45, 50, 55, 60};

  const std::vector<std::pair<
      std::string, std::function<char *(const char *, const char *, size_t)>>>
      strnstr_impls = {
          {"ndpi_strnstr", ndpi_strnstr},
          {"ndpi_strnstr_opt", ndpi_strnstr_opt},
      };

  const int iterations = 100000;
  const int warm_up_iterations = 1000;

  for (size_t haystack_len : haystack_lengths) {
    for (size_t needle_len : needle_lengths) {
      std::cout << "\nTest case - Haystack length: " << haystack_len
                << ", Needle length: " << needle_len << "\n";

      std::string haystack = random_string(haystack_len, gen);
      std::string needle = random_string(needle_len, gen);

      std::map<std::string, double> times;

      for (const auto &impl : strnstr_impls) {
        warm_up(impl.second, haystack, needle, warm_up_iterations);

        std::vector<double> times_vector;
        for (int i = 0; i < iterations; i++) {
          times_vector.push_back(measure_time(impl.second, haystack, needle));
        }

        double average_time = average_without_extremes(times_vector);

        times[impl.first] = average_time;
        std::cout << "Average time for " << impl.first << ": " << average_time
                  << " ns\n";
      }

      std::string fastest_impl;
      double fastest_time = std::numeric_limits<double>::max();
      for (const auto &impl_time : times) {
        if (impl_time.second < fastest_time) {
          fastest_impl = impl_time.first;
          fastest_time = impl_time.second;
        }
      }

      for (const auto &impl_time : times) {
        if (impl_time.first != fastest_impl) {
          std::cout << fastest_impl << " is " << impl_time.second / fastest_time
                    << " times faster than " << impl_time.first << "\n";
        }
      }
    }
  }

  return 0;
}