| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  AutoTime.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2018/07/14.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-10 21:08:55 +08:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							|  |  |  | #include <Windows.h>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | #include <sys/time.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-10 21:08:55 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include <MNN/AutoTime.hpp>
 | 
					
						
							|  |  |  | #include "core/Macro.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Timer::Timer() { | 
					
						
							|  |  |  |     reset(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Timer::~Timer() { | 
					
						
							|  |  |  |     // do nothing
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Timer::reset() { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							|  |  |  |     LARGE_INTEGER time, freq; | 
					
						
							|  |  |  |     QueryPerformanceFrequency(&freq); | 
					
						
							|  |  |  |     QueryPerformanceCounter(&time); | 
					
						
							|  |  |  |     uint64_t sec   = time.QuadPart / freq.QuadPart; | 
					
						
							|  |  |  |     uint64_t usec  = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; | 
					
						
							|  |  |  |     mLastResetTime = sec * 1000000 + usec; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     struct timeval Current; | 
					
						
							|  |  |  |     gettimeofday(&Current, nullptr); | 
					
						
							|  |  |  |     mLastResetTime = Current.tv_sec * 1000000 + Current.tv_usec; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint64_t Timer::durationInUs() { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | #if defined(_MSC_VER)
 | 
					
						
							|  |  |  |     LARGE_INTEGER time, freq; | 
					
						
							|  |  |  |     QueryPerformanceCounter(&time); | 
					
						
							|  |  |  |     QueryPerformanceFrequency(&freq); | 
					
						
							|  |  |  |     uint64_t sec  = time.QuadPart / freq.QuadPart; | 
					
						
							|  |  |  |     uint64_t usec = (time.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; | 
					
						
							|  |  |  |     auto lastTime = sec * 1000000 + usec; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     struct timeval Current; | 
					
						
							|  |  |  |     gettimeofday(&Current, nullptr); | 
					
						
							|  |  |  |     auto lastTime = Current.tv_sec * 1000000 + Current.tv_usec; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  |     return lastTime - mLastResetTime; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | AutoTime::AutoTime(int line, const char* func) : Timer() { | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     mName = ::strdup(func); | 
					
						
							|  |  |  |     mLine = line; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | AutoTime::~AutoTime() { | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  |     auto timeInUs = durationInUs(); | 
					
						
							|  |  |  |     MNN_PRINT("%s, %d, cost time: %f ms\n", mName, mLine, (float)timeInUs / 1000.0f); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |     free(mName); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } // namespace MNN
 |