-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.cpp
More file actions
50 lines (44 loc) · 1.59 KB
/
Copy pathdriver.cpp
File metadata and controls
50 lines (44 loc) · 1.59 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
#include <random>
#include <chrono>
#include <array>
#include <iostream>
#include <algorithm>
int main()
{
const auto repeat_times = 2000U;
const auto alloc_count = 10000U;
const auto max_alloc_size = 1000U;
auto clock = std::chrono::high_resolution_clock();
std::cout << "Profiling random size allocation/deletion between 0 to " << max_alloc_size << " bytes block "
<< alloc_count * repeat_times << " times..." << std::endl;
// generate random size for testing
std::array<int, alloc_count> sizes;
std::generate(sizes.begin(), sizes.end(), [max_alloc_size]() {return rand() % max_alloc_size;});
// measure original allocation speed
auto start = clock.now();
for (int j = 0; j < repeat_times; ++j)
{
for (int i = 0; i < alloc_count; ++i)
{
auto addr = malloc(sizes[i]);
free(addr);
}
}
auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(clock.now() - start);
std::cout << "Original allocation performance:" << std::endl;
std::cout << diff.count() << " milliseconds" << std::endl;
// measure improved allocation speed
auto start2 = clock.now();
for (int j = 0; j < repeat_times; ++j)
{
for (unsigned i = 0; i < alloc_count; ++i)
{
auto addr = new char[sizes[i]];
delete[] addr;
}
}
auto diff2 = std::chrono::duration_cast<std::chrono::milliseconds>(clock.now() - start2);
std::cout << "Improved allocation performance:" << std::endl;
std::cout << diff2.count() << " milliseconds" << std::endl;
std::cout << "Improved allocation is " << (double)diff.count() / diff2.count() << " times faster." << std::endl;
}