Skip to content

Instantly share code, notes, and snippets.

@ahmadyan
Created September 6, 2012 01:07
Show Gist options
  • Save ahmadyan/3649349 to your computer and use it in GitHub Desktop.
Save ahmadyan/3649349 to your computer and use it in GitHub Desktop.
tick-tock
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <string>
#include "utils.h"
using namespace std;
namespace utils {
double initial_time = clock();
time_t t1=0,t2=0,t3=0;
double final_time = 0;
double total_time = 0;
/*
the tick & tock functions are used toghether (usually in main() ) to
calculate the time used by a specific function.
they should be used like this:
utils::tick();
f1();
f2();
f3();
utils::tock("name of process/function", result_file_pointer);
*/
void tick(){
initial_time = clock();
time(&t1);
}
void tock(std::string processName, FILE* resultfp){
final_time = clock();
total_time = (double) (final_time - initial_time) / (double) CLOCKS_PER_SEC ;
time(&t2);
t3 = t2 - t1;
if (t3>1000) total_time = t3;
cout << endl << "[time-report] Time required to " << processName << " is: " << total_time << " seconds"<<endl;
if(resultfp!=NULL) fprintf(resultfp, "\n[time-report] Time required to compute %s is: %f seconds.\n", processName.c_str(), total_time);
}
} // namespace utils
#include <vector>
#include <string>
#include <iostream>
/// namespace to contain the used utilities
namespace utils {
void tick();
void tock(std::string processName,FILE* resultfp);
} // namespace utils
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment