-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest03_func_global.cpp
45 lines (39 loc) · 1015 Bytes
/
test03_func_global.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
#include <mutex> // std::mutex/lock_guard
#include <stdint.h> // uint64_t
#include <stdio.h> // printf
#include <string.h> // memset
#include "rdtsc.h" // rdtsc
char buf[80];
uint64_t memset_duration;
uint64_t plain_loop_duration;
std::mutex mutex;
void test_memset()
{
uint64_t t1 = rdtsc();
memset(buf, 0, sizeof buf);
uint64_t t2 = rdtsc();
memset_duration += (t2 - t1);
}
void test_plain_loop()
{
uint64_t t1 = rdtsc();
for (size_t j = 0; j < sizeof buf; ++j) {
buf[j] = 0;
}
uint64_t t2 = rdtsc();
plain_loop_duration += (t2 - t1);
}
int main()
{
constexpr int LOOPS = 10000000;
for (int i = 0; i < LOOPS; ++i) {
std::lock_guard guard{mutex};
test_memset();
}
printf("%g\n", memset_duration * 1.0 / LOOPS);
for (int i = 0; i < LOOPS; ++i) {
std::lock_guard guard{mutex};
test_plain_loop();
}
printf("%g\n", plain_loop_duration * 1.0 / LOOPS);
}