-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (55 loc) · 2.24 KB
/
main.cpp
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
#include <iostream>
#include <cstring>
#include <chrono>
#include "s7c_benchmark.hpp"
#define N_TIMES 100000000
void usage(char * call);
int main(int argc, char * argv[])
{
if (argc != 3 && argc != 4) usage(argv[0]);
unsigned int n_times = N_TIMES;
if (argc == 4) n_times = atoi(argv[3]);
std::string test_str_a = argv[1];
std::string test_str_b = argv[2];
Timer directT("direct == string comp", false);
Timer stringT("string.compare", false);
Timer strcmpT("strcmp", false);
Timer memcmpT("memcmp", false);
Timer lmemcmpT("length_and_memory_argcmp", false);
bool res_direct = false;
bool res_string = false;
bool res_strcmp = false;
bool res_memcpy = false;
bool res_lmemcpy = false;
for (unsigned int i = 0; i < n_times; ++i)
{
directT.start();
if (test_str_a == test_str_b) res_direct = true;
directT.stop();
stringT.start();
if (!test_str_a.compare(test_str_b)) res_string = true;
stringT.stop();
strcmpT.start();
if (!strcmp(argv[1], argv[2])) res_strcmp = true;
strcmpT.stop();
int arglen = strlen(argv[2]);
memcmpT.start();
if (!memcmp(argv[1], argv[2], arglen)) res_memcpy = true;
memcmpT.stop();
lmemcmpT.start();
if (*(argv[1] + arglen) == '\0' && !memcmp(argv[1], argv[2], arglen)) res_lmemcpy = true;
lmemcmpT.stop();
}
std::cout << std::fixed << std::boolalpha;
std::cout << "direct == comp: " << res_direct << " in " << directT.get_sum() / n_times << " nanosec" << std::endl;
std::cout << "string.compare: " << res_string << " in " << stringT.get_sum() / n_times << " nanosec" << std::endl;
std::cout << "strcmp: " << res_strcmp << " in " << strcmpT.get_sum() / n_times << " nanosec" << std::endl;
std::cout << "memcmp: " << res_memcpy << " in " << memcmpT.get_sum() / n_times << " nanosec" << std::endl;
std::cout << "length_and_memory_argcmp: " << res_lmemcpy << " in " << lmemcmpT.get_sum() / n_times << " nanosec" << std::endl;
return 0;
}
void usage(char * call)
{
std::cout << "USAGE:" << "\t" << call << " <string_a> <string_b> [n_iterations]" << std::endl;
exit(0);
}