Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- TIMER :: http://bit.ly/cpp-timer
- Pomoćni alat za mjerenje trajanja izvršavanja programa
- Kompajlirati u timer.exe
- ---------------------------------------------------------
- pozivati iz konzole na sljedeći način:
- timer start
- program-koji-testiramo.exe
- timer
- ---------------------------------------------------------
- ili u jednoj liniji:
- timer start & program-koji-testiramo.exe & timer
- ---------------------------------------------------------
- ili iz samog programa za mjerenje trajanja jednog dijela programa:
- int main() {
- system("timer start");
- // neki dio programa, npr. petlja
- system("timer");
- }
- */
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- #include <sstream>
- #include <string>
- #ifdef WIN32
- #include <Windows.h>
- #else
- #include <sys/time.h>
- #include <ctime>
- #endif
- using namespace std;
- /* Returns the amount of milliseconds elapsed since the UNIX epoch. Works on both
- * windows and linux. */
- typedef long long int int64;
- typedef unsigned long long int uint64;
- typedef unsigned int uint;
- int64 GetTimeMs64()
- {
- #ifdef WIN32
- /* Windows */
- FILETIME ft;
- LARGE_INTEGER li;
- /* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
- * to a LARGE_INTEGER structure. */
- GetSystemTimeAsFileTime(&ft);
- li.LowPart = ft.dwLowDateTime;
- li.HighPart = ft.dwHighDateTime;
- uint64 ret = li.QuadPart;
- ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
- ret /= 10000; /* From 100 nano seconds (10^-7) to 1 millisecond (10^-3) intervals */
- return ret;
- #else
- /* Linux */
- struct timeval tv;
- gettimeofday(&tv, NULL);
- uint ret = tv.tv_usec;
- /* Convert from micro seconds (10^-6) to milliseconds (10^-3) */
- ret /= 1000;
- /* Adds the seconds (10^0) after converting them to milliseconds (10^-3) */
- ret += (tv.tv_sec * 1000);
- return ret;
- #endif
- }
- string int2string(int64 i) {
- stringstream ss;//create a stringstream
- ss << i;//add number to the stream
- return ss.str();//return a string with the contents of the stream
- }
- int64 string2int(string s) {
- stringstream ss;//create a stringstream
- ss << s;
- int64 out;
- ss >> out;
- return out;
- }
- #include <iostream>
- #include <fstream>
- string readfile()
- {
- fstream fin;
- string word;
- string testfile = "timer.txt";
- fin.open(testfile.c_str(), ios::in);
- if (fin.is_open()) {
- std::getline(fin, word, ' ');
- return word;
- }
- return "";
- }
- void writefile(string x) {
- ofstream fout;
- string word;
- string testfile = "timer.txt";
- fout.open(testfile.c_str(), ios::out);
- if (fout.is_open()) {
- fout << x;
- }
- }
- int main(int argc, char **argv) {
- if (argc >1) {
- string x = string(argv[1]);
- if (x == "start") {
- writefile(int2string(GetTimeMs64()));
- }
- } else {
- int64 starttime = string2int(readfile());
- cout << "Execution took " << (GetTimeMs64() - starttime) << " ms" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement