Last active
January 16, 2016 14:18
-
-
Save hG3n/808af3755d5d17e058f2 to your computer and use it in GitHub Desktop.
this is a short snippet to remind how one can calculate time needed by one process
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 <ctime> | |
#include <vector> | |
#include <algorithm> | |
int main() { | |
// create vector for storing the calculated times needed for a piece of code to finish | |
std::vector<float> times; | |
int counter = 0; | |
// small while loop to get reckognizable results | |
while(counter < 1000) { | |
auto start = clock(); | |
std::vector<int> v; | |
for(int i = 0; i < 1000; ++i) { | |
v.push_back(i); | |
} | |
auto end = clock(); | |
// calculate the bygone time | |
float seconds = (float)(end-start)/CLOCKS_PER_SEC; | |
times.push_back(seconds); | |
++counter; | |
} | |
// calculate the mean bygone time | |
float total = 0.0f; | |
std::for_each(times.begin(), times.end(), [&total](float value){ | |
total += value; | |
}); | |
float average = total/times.size(); | |
std::cout << average << std::endl; | |
// number of operations per second | |
std::cout << 60/average << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment