Created
September 12, 2016 17:10
-
-
Save harmishhk/43e472cc90c9c4a681c6d4419375ea8e 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
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <random> | |
#include <chrono> | |
#include <ctime> | |
int main() | |
{ | |
int start_c = 10000, end_c = 20000; | |
std::random_device rd; | |
std::mt19937 mt(rd()); | |
std::uniform_real_distribution<double> dist(1.0, 10.0); | |
std::chrono::time_point<std::chrono::system_clock> start, end; | |
double total_b = 0.0, total_c = 0.0; | |
double value = dist(mt); | |
for (int size = start_c; size < end_c; ++size) | |
{ | |
std::vector<double> b, c; | |
start = std::chrono::system_clock::now(); | |
b.resize(size); | |
for (int i = 0; i < size; ++i) | |
b[i] = value; | |
end = std::chrono::system_clock::now(); | |
std::chrono::duration<double> elapsed_seconds = end - start; | |
total_b += elapsed_seconds.count(); | |
start = std::chrono::system_clock::now(); | |
for (int i = 0; i < size; ++i) | |
c.push_back(value); | |
end = std::chrono::system_clock::now(); | |
elapsed_seconds = end - start; | |
total_c += elapsed_seconds.count(); | |
} | |
std::cout << std::fixed << "elapsed time average resize\t: " << total_b / (end_c - start_c) << "s\n"; | |
std::cout << "elapsed time average push_back\t: " << total_c / (end_c - start_c) << "s\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment