-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_allocate.cpp
48 lines (39 loc) · 1.57 KB
/
mem_allocate.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
#include <stdlib.h>
#include <stddef.h>
#include <memory>
#include <vector>
#include <chrono>
#include <iostream>
/**
* Allocator template to align buffer to Page boundary for better data transfer
*/
template <typename T>
struct aligned_allocator {
using value_type = T;
T* allocate(std::size_t num) {
void* ptr = nullptr;
if (posix_memalign(&ptr, 4096, num*sizeof(T))) {
throw std::bad_alloc();
}
return reinterpret_cast<T*>(ptr);
}
void deallocate(T* p, std::size_t num) {
free(p);
}
};
int main ( int argc, char * argv [] ) {
size_t size = std::stoll(argv[1]);
size_t size2 = std::stoll(argv[2]);
// has to use O2 to "fast" allocate
auto aligned_alloc_time_begin = std::chrono::high_resolution_clock::now();
auto mem = std::vector<unsigned char, aligned_allocator<unsigned char>>(1ULL<<size);
auto aligned_alloc_time_end = std::chrono::high_resolution_clock::now();
auto mem_alloc_time_begin = std::chrono::high_resolution_clock::now();
auto mem2 = std::vector<unsigned char>(1ULL<<size2);
auto mem_alloc_time_end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> aligned_duration = aligned_alloc_time_end - aligned_alloc_time_begin;
std::chrono::duration<double> non_aligned_duration = mem_alloc_time_end - mem_alloc_time_begin;
std::cout << "TIME aligned mem allocation : " << aligned_duration.count() << "s" << std::endl;
std::cout << "TIME non aligned mem allocation : " << non_aligned_duration.count() << "s" << std::endl;
return 0;
}