source: XmlTools2/trunk/libs/time_logger.h@ 921

Last change on this file since 921 was 906, checked in by s10k, 11 years ago
File size: 3.8 KB
RevLine 
[906]1/*
2 * 42TinyJS
3 *
4 * A fork of TinyJS with the goal to makes a more JavaScript/ECMA compliant engine
5 *
6 * Authored By Armin Diedering <armin@diedering.de>
7 *
8 * Copyright (C) 2010-2013 ardisoft
9 *
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy of
12 * this software and associated documentation files (the "Software"), to deal in
13 * the Software without restriction, including without limitation the rights to
14 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
15 * of the Software, and to permit persons to whom the Software is furnished to do
16 * so, subject to the following conditions:
17
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30
31#ifndef time_logger_h__
32#define time_logger_h__
33#if defined(WITH_TIME_LOGGER)
34
35#include <stdint.h>
36#include <stdio.h>
37#include <string>
38#ifdef _WIN32
39#include <Windows.h>
40#else
41#include <time.h>
42#endif
43class TimeLogger {
44public:
45 TimeLogger(const char *Name, bool Started=false, const char *extName=0)
46 :
47 name(Name), start_time(gettime()), sum_time(0), calls(0), started(Started) {
48 if(extName) {
49 name+="[";
50 name+=extName;
51 name+="]";
52 }
53 }
54 TimeLogger(const char *Name, const char *extName, bool Started=false)
55 :
56 name(Name), start_time(gettime()), sum_time(0), calls(0), started(Started) {
57 if(extName) {
58 name+="[";
59 name+=extName;
60 name+="]";
61 }
62 }
63 ~TimeLogger() {
64 printLog();
65// getchar();
66 }
67 void startTimer() {
68 start_time = gettime();
69 started = true;
70 }
71 void stopTimer() {
72 if(!started) return;
73 sum_time += gettime()-start_time;
74 calls++;
75 started = false;
76 }
77 void printLog() {
78 if(started) stopTimer();
79 if(calls == 1)
80 printf("Timer( %s ) = %d,%06d sec \n",
81 name.c_str(), (int)(sum_time / 1000000LL), (int)(sum_time % 1000000LL));
82 else if(calls>1)
83 printf("Timer( %s ) = %d,%06d sec (called %d times) -> %.d microsec per call\n",
84 name.c_str(), (int)(sum_time / 1000000LL), (int)(sum_time % 1000000LL),
85 calls, (int)(sum_time/calls));
86 calls = 0; sum_time = 0;
87 }
88private:
89// static int64_t frequenzy = 0;
90 std::string name;
91 int64_t start_time, sum_time;
92 uint32_t calls;
93 bool started;
94 int64_t gettime() { // set out to time in millisec
95#ifdef _WIN32
96 static LARGE_INTEGER fr = {0};
97 LARGE_INTEGER li;
98 if(fr.QuadPart == 0) QueryPerformanceFrequency(&fr);
99 QueryPerformanceCounter(&li);
100 return (li.QuadPart * 1000000LL) / fr.QuadPart;
101#else
102 return (clock() * 1000000LL) / CLOCKS_PER_SEC;
103#endif
104 };
105};
106class _TimeLoggerHelper {
107public:
108 _TimeLoggerHelper(TimeLogger &Tl) : tl(Tl) { tl.startTimer(); }
109 ~_TimeLoggerHelper() { tl.stopTimer(); }
110private:
111 TimeLogger &tl;
112};
113# define TimeLoggerCreate(a, ...) TimeLogger a##_TimeLogger(#a,##__VA_ARGS__)
114# define TimeLoggerStart(a) a##_TimeLogger.startTimer()
115# define TimeLoggerStop(a) a##_TimeLogger.stopTimer()
116# define TimeLoggerLogprint(a) a##_TimeLogger.printLog()
117# define TimeLoggerHelper(a) _TimeLoggerHelper a##_helper(a##_TimeLogger)
118#else /* _DEBUG */
119# define TimeLoggerCreate(...)
120# define TimeLoggerStart(...) do{}while(0)
121# define TimeLoggerStop(...) do{}while(0)
122# define TimeLoggerLogprint(a) do{}while(0)
123# define TimeLoggerHelper(a) do{}while(0)
124#endif /* _DEBUG */
125
126
127
128#endif // time_logger_h__
Note: See TracBrowser for help on using the repository browser.