You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.3 KiB

//
// Created by yeler082 on 2024/02/20.
//
#ifndef __ARITH_TIMER_H_
#define __ARITH_TIMER_H_
#include <stdio.h>
#ifdef __linux__
#include <sys/time.h> // for gettimeofday()
#elif _WIN32
#include <time.h>
#include <winsock.h>
#endif
#ifdef WIN32
#define gettimeofday(tp, tzp) \
do {\
time_t clock; struct tm tm; SYSTEMTIME wtm; GetLocalTime(&wtm);\
tm.tm_year = wtm.wYear - 1900;\
tm.tm_mon = wtm.wMonth - 1;\
tm.tm_mday = wtm.wDay;\
tm.tm_hour = wtm.wHour;\
tm.tm_min = wtm.wMinute;\
tm.tm_sec = wtm.wSecond;\
tm.tm_isdst = -1;\
clock = mktime(&tm); (tp)->tv_sec = clock; (tp)->tv_usec = wtm.wMilliseconds * 1000;\
} while (0)
#endif
struct time_checker
{
struct timeval start_time;
struct timeval stop_time;
void TimeStart()
{
gettimeofday(&start_time, nullptr);
}
void TimeStop()
{
gettimeofday(&stop_time, nullptr);
}
int timeDistance()
{
long time_1_token = start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
long time_2_token = stop_time.tv_sec * 1000 + stop_time.tv_usec / 1000;
return time_2_token - time_1_token;
}
void show_distance(const char*title = "current time")
{
long time_1_token = start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
long time_2_token = stop_time.tv_sec * 1000 + stop_time.tv_usec / 1000;
printf("%s : %ld ms\n", title, time_2_token - time_1_token);
}
void show_ns_distance(const char*title = "current time")
{
long time_1_token = start_time.tv_sec * 1000000 + start_time.tv_usec;
long time_2_token = stop_time.tv_sec * 1000000 + stop_time.tv_usec;
printf("%s : %ld ns\n", title, time_2_token - time_1_token);
}
bool timeout(int second)
{
struct timeval current;
gettimeofday(&current, nullptr);
long time_1_token = start_time.tv_sec * 1000 + start_time.tv_usec / 1000;
long time_2_token = current.tv_sec * 1000 + current.tv_usec / 1000;
int value = time_2_token - time_1_token;
return value > second;
}
static long get_current_timetoken()
{
struct timeval current;
gettimeofday(&current, nullptr);
return (current.tv_sec * 1000 + current.tv_usec / 1000);
}
};
#endif //BABY_FACE_NCNN_DEMO_TIMER_H