Advertisement
kburnik

C++ TIMER - mjerenje trajanja izvršavanja programa

Dec 15th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. /*
  2. TIMER :: http://bit.ly/cpp-timer
  3. Pomoćni alat za mjerenje trajanja izvršavanja programa
  4. Kompajlirati u timer.exe
  5. ---------------------------------------------------------
  6. pozivati iz konzole na sljedeći način:
  7.  
  8. timer start
  9. program-koji-testiramo.exe
  10. timer
  11.  
  12. ---------------------------------------------------------
  13. ili u jednoj liniji:
  14.  
  15. timer start & program-koji-testiramo.exe & timer
  16.  
  17. ---------------------------------------------------------
  18. ili iz samog programa za mjerenje trajanja jednog dijela programa:
  19.  
  20. int main() {
  21.   system("timer start");
  22.   // neki dio programa, npr. petlja
  23.   system("timer");
  24. }
  25. */
  26.  
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <algorithm>
  30. #include <sstream>
  31. #include <string>
  32.  
  33.  
  34. #ifdef WIN32
  35. #include <Windows.h>
  36. #else
  37. #include <sys/time.h>
  38. #include <ctime>
  39. #endif
  40.  
  41. using namespace std;
  42.  
  43. /* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
  44.  * windows and linux. */
  45.  
  46. typedef long long int int64;
  47. typedef unsigned long long int uint64;
  48. typedef unsigned int uint;
  49.  
  50. int64 GetTimeMs64()
  51. {
  52. #ifdef WIN32
  53.  /* Windows */
  54.  FILETIME ft;
  55.  LARGE_INTEGER li;
  56.  
  57.  /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
  58.   * to a LARGE_INTEGER structure. */
  59.  GetSystemTimeAsFileTime(&ft);
  60.  li.LowPart = ft.dwLowDateTime;
  61.  li.HighPart = ft.dwHighDateTime;
  62.  
  63.  uint64 ret = li.QuadPart;
  64.  ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
  65.  ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
  66.  
  67.  return ret;
  68. #else
  69.  /* Linux */
  70.  struct timeval tv;
  71.  
  72.  gettimeofday(&tv, NULL);
  73.  
  74.  uint ret = tv.tv_usec;
  75.  /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
  76.  ret /= 1000;
  77.  
  78.  /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
  79.  ret += (tv.tv_sec * 1000);
  80.  
  81.  return ret;
  82. #endif
  83. }
  84.  
  85. string int2string(int64 i) {
  86.    stringstream ss;//create a stringstream
  87.    ss << i;//add number to the stream
  88.    return ss.str();//return a string with the contents of the stream
  89. }
  90.  
  91. int64 string2int(string s) {
  92.     stringstream ss;//create a stringstream
  93.     ss << s;
  94.     int64 out;
  95.     ss >> out;
  96.     return out;
  97. }
  98.  
  99.  
  100. #include <iostream>
  101. #include <fstream>
  102.  
  103. string readfile()
  104. {
  105.     fstream fin;
  106.     string word;
  107.     string testfile = "timer.txt";
  108.     fin.open(testfile.c_str(), ios::in);
  109.     if (fin.is_open()) {
  110.         std::getline(fin, word, ' ');
  111.         return word;
  112.     }
  113.     return "";
  114. }
  115.  
  116. void writefile(string x) {
  117.     ofstream fout;
  118.     string word;
  119.     string testfile = "timer.txt";
  120.     fout.open(testfile.c_str(), ios::out);
  121.     if (fout.is_open()) {
  122.         fout << x;
  123.     }
  124.  
  125. }
  126.  
  127. int main(int argc, char **argv) {
  128.    
  129.     if (argc >1) {
  130.        string x = string(argv[1]);
  131.        if (x == "start") {
  132.             writefile(int2string(GetTimeMs64()));
  133.        }
  134.     } else {
  135.         int64 starttime = string2int(readfile());
  136.         cout << "Execution took " << (GetTimeMs64() - starttime) << " ms" << endl;
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement