Skip to content

Instantly share code, notes, and snippets.

@hG3n
Last active January 16, 2016 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hG3n/808af3755d5d17e058f2 to your computer and use it in GitHub Desktop.
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
#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