-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefilter.cpp
More file actions
51 lines (43 loc) · 1.62 KB
/
Copy pathprefilter.cpp
File metadata and controls
51 lines (43 loc) · 1.62 KB
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
// Le moteur retente-t-il un match a chaque position, ou sait-il sauter ?
// Texte sans aucun chiffre => 0 match possible. Tout le temps mesure est donc
// du pur "cout des tentatives echouees".
#define NOMINMAX
#include <windows.h>
#include <chrono>
#include <cstdio>
#include <regex>
#include <string>
typedef std::chrono::steady_clock clk;
template <class F>
static void bench(const char* name, F fn) {
long m = fn();
auto t0 = clk::now();
int passes = 0;
double el = 0;
do { m = fn(); ++passes; el = std::chrono::duration<double>(clk::now() - t0).count(); }
while (el < 1.0);
double ms = el / passes * 1000.0;
printf(" %-44s %8.2f ms %5ld matches\n", name, ms, m);
}
int main() {
const size_t N = 303104; // meme taille que le texte de logs
std::string plain(N, 'x');
for (size_t i = 0; i < N; ++i) plain[i] = (char)('a' + (i * 7 + i / 13) % 26);
for (size_t i = 79; i < N; i += 80) plain[i] = '\n';
printf("\n texte sans chiffre : %.1f Ko, aucun match possible\n\n", N / 1024.0);
std::regex rx(R"((\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}))",
std::regex::ECMAScript | std::regex::optimize);
bench("std::regex sur texte sans chiffre", [&]() -> long {
long n = 0;
for (auto it = std::sregex_iterator(plain.begin(), plain.end(), rx);
it != std::sregex_iterator(); ++it) ++n;
return n;
});
bench("boucle triviale : compter les chiffres", [&]() -> long {
long n = 0;
for (size_t i = 0; i < N; ++i) if (plain[i] >= '0' && plain[i] <= '9') ++n;
return n;
});
printf("\n");
return 0;
}