Last active
July 29, 2018 09:29
-
-
Save SmallJoker/0306777070b4f6e82e9c8eadecf4a7dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ vectortest.cpp | |
/* | |
emplace_back: 553305 ticks | |
push_back: 665359 ticks | |
resize: 437487 ticks | |
*/ | |
// g++ -O3 -ffast-math vectortest.cpp | |
/* | |
emplace_back: 165961 ticks | |
push_back: 165695 ticks | |
resize: 87059 ticks | |
*/ | |
#include <iostream> | |
#include <vector> | |
#include <ctime> | |
#include <cstdint> | |
#define LOG(what, start, end) \ | |
(end) = clock(); \ | |
std::cout << (what) << ((end) - (start)) << " ticks" << std::endl; | |
inline void write4bytes(uint8_t *data) | |
{ | |
data[0] = 12; | |
data[1] = 75; | |
data[2] = 22; | |
data[3] = 132; | |
} | |
int main() | |
{ | |
clock_t start, end; | |
uint32_t i; | |
{ | |
// Pre-heat a little | |
std::vector<uint8_t> buf; | |
for (i = 0; i < 1000000; ++i) | |
buf.push_back((uint8_t)i); | |
} | |
{ | |
std::vector<uint8_t> buf; | |
start = clock(); | |
for (i = 0; i < 1000000; ++i) { | |
buf.emplace_back(1); | |
buf.emplace_back(5); | |
buf.emplace_back(8); | |
buf.emplace_back(2); | |
buf.emplace_back(52); | |
buf.emplace_back(122); | |
buf.emplace_back(240); | |
buf.emplace_back(235); | |
buf.emplace_back(34); | |
buf.emplace_back(182); // 10 | |
buf.emplace_back(1); | |
buf.emplace_back(5); | |
buf.emplace_back(8); | |
buf.emplace_back(2); | |
buf.emplace_back(52); | |
buf.emplace_back(122); | |
buf.emplace_back(240); | |
buf.emplace_back(235); | |
buf.emplace_back(34); | |
buf.emplace_back(182); // 20 | |
} | |
LOG("emplace_back: ", start, end) | |
} | |
{ | |
std::vector<uint8_t> buf; | |
start = clock(); | |
for (i = 0; i < 1000000; ++i) { | |
buf.push_back(1); | |
buf.push_back(5); | |
buf.push_back(8); | |
buf.push_back(2); | |
buf.push_back(52); | |
buf.push_back(122); | |
buf.push_back(240); | |
buf.push_back(235); | |
buf.push_back(34); | |
buf.push_back(182); // 10 | |
buf.push_back(1); | |
buf.push_back(5); | |
buf.push_back(8); | |
buf.push_back(2); | |
buf.push_back(52); | |
buf.push_back(122); | |
buf.push_back(240); | |
buf.push_back(235); | |
buf.push_back(34); | |
buf.push_back(182); // 20 | |
} | |
LOG("push_back: ", start, end) | |
} | |
{ | |
std::vector<uint8_t> buf; | |
start = clock(); | |
uint32_t bufsize = 0; | |
for (i = 0; i < 1000000; ++i) { | |
buf.resize(bufsize + 4); | |
write4bytes(&buf[bufsize]); | |
bufsize += 4; | |
buf.resize(bufsize + 4); | |
write4bytes(&buf[bufsize]); | |
bufsize += 4; | |
buf.resize(bufsize + 4); | |
write4bytes(&buf[bufsize]); | |
bufsize += 4; | |
buf.resize(bufsize + 4); | |
write4bytes(&buf[bufsize]); | |
bufsize += 4; | |
buf.resize(bufsize + 4); | |
write4bytes(&buf[bufsize]); | |
bufsize += 4; | |
} | |
LOG("resize: ", start, end) | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment