-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushback.cpp
43 lines (34 loc) · 1023 Bytes
/
pushback.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
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <vector>
void withArray(std::vector<uint8_t> &vec, std::size_t const size) {
std::cout << "withArray" << std::endl;
uint8_t *array = new uint8_t[size];
for (std::size_t i = 0; i < size; ++i)
array[i] = 0;
vec.insert(vec.end(), array, array + size);
delete array;
}
void withIndex(std::vector<uint8_t> &vec, std::size_t const size) {
std::cout << "withIndex" << std::endl;
vec.resize(size);
for (std::size_t i = 0; i < size; ++i)
vec[i] = 0;
}
void withPushback(std::vector<uint8_t> &vec, std::size_t const size) {
std::cout << "withPushback" << std::endl;
vec.reserve(size);
for (std::size_t i = 0; i < size; ++i)
vec.push_back(0);
}
int main(int argc, char *argv[]) {
std::size_t TOTAL = 3000000000; // ~5GB
std::vector<uint8_t> vec;
// use one of these
// withArray(vec, TOTAL);
// withIndex(vec, TOTAL);
withPushback(vec, TOTAL);
std::cout << "length " << vec.size() << std::endl;
return 0;
}