1 //
2 // AutoTime.cpp
3 // MNN
4 //
5 // Created by MNN on 2018/07/14.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8
9 #include <stdlib.h>
10 #include <string.h>
11 #if defined(_MSC_VER)
12 #include <Windows.h>
13 #else
14 #include <sys/time.h>
15 #endif
16 #include <MNN/AutoTime.hpp>
17 #include "core/Macro.h"
18
19 namespace MNN {
20
Timer()21 Timer::Timer() {
22 reset();
23 }
24
~Timer()25 Timer::~Timer() {
26 // do nothing
27 }
28
reset()29 void Timer::reset() {
30 #if defined(_MSC_VER)
31 LARGE_INTEGER time, freq;
32 QueryPerformanceFrequency(&freq);
33 QueryPerformanceCounter(&time);
34 uint64_t sec = time.QuadPart / freq.QuadPart;
35 uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart;
36 mLastResetTime = sec * 1000000 + usec;
37 #else
38 struct timeval Current;
39 gettimeofday(&Current, nullptr);
40 mLastResetTime = Current.tv_sec * 1000000 + Current.tv_usec;
41 #endif
42 }
43
durationInUs()44 uint64_t Timer::durationInUs() {
45 #if defined(_MSC_VER)
46 LARGE_INTEGER time, freq;
47 QueryPerformanceCounter(&time);
48 QueryPerformanceFrequency(&freq);
49 uint64_t sec = time.QuadPart / freq.QuadPart;
50 uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart;
51 auto lastTime = sec * 1000000 + usec;
52 #else
53 struct timeval Current;
54 gettimeofday(&Current, nullptr);
55 auto lastTime = Current.tv_sec * 1000000 + Current.tv_usec;
56 #endif
57
58 return lastTime - mLastResetTime;
59 }
60
AutoTime(int line,const char * func)61 AutoTime::AutoTime(int line, const char* func) : Timer() {
62 mName = ::strdup(func);
63 mLine = line;
64 }
~AutoTime()65 AutoTime::~AutoTime() {
66 auto timeInUs = durationInUs();
67 MNN_PRINT("%s, %d, cost time: %f ms\n", mName, mLine, (float)timeInUs / 1000.0f);
68 free(mName);
69 }
70
71 } // namespace MNN
72