From 84171fea33e983a956ab362b606a69805495318d Mon Sep 17 00:00:00 2001 From: Scheaven <xuepengqiang> Date: 星期二, 27 七月 2021 17:35:32 +0800 Subject: [PATCH] up timer --- /dev/null | 38 --------- src/core/ari_manager.cpp | 3 src/h_interface.cpp | 4 + src/utils/time_util.cpp | 134 +++++++++++++++++++++++++++++++++ src/utils/time_util.h | 49 ++++++++++++ src/detecter_tools/model.h | 2 6 files changed, 188 insertions(+), 42 deletions(-) diff --git a/src/core/ari_manager.cpp b/src/core/ari_manager.cpp index f6db85e..4f81d92 100644 --- a/src/core/ari_manager.cpp +++ b/src/core/ari_manager.cpp @@ -57,10 +57,7 @@ // cv::imshow("img",image0); // cv::waitKey(0); - Timer timer; - timer.reset(); this->detector->detect(batch_img, batch_res); - timer.out("detect"); t_result.targets = (Target*)malloc(sizeof(Target)*batch_res[0].size()); // 灏嗙畻娉曠粨鏋滆浆鍖栦负鏍囧噯鐨勬牸寮忥紙浠ョ洰鏍囨娴嬩负渚嬶級 diff --git a/src/detecter_tools/model.h b/src/detecter_tools/model.h index 5fd61ad..b23cca9 100644 --- a/src/detecter_tools/model.h +++ b/src/detecter_tools/model.h @@ -13,7 +13,7 @@ #include <stdint.h> #include <string> #include <vector> -#include "../utils/timer_utils.hpp" +#include "../utils/time_util.h" #include "../config.h" #include "opencv2/opencv.hpp" diff --git a/src/h_interface.cpp b/src/h_interface.cpp index ce84aa2..62c867f 100644 --- a/src/h_interface.cpp +++ b/src/h_interface.cpp @@ -16,6 +16,7 @@ CLog::Initialize("../config/log4cplus.properties"); ReadJsonFromFile(conf); AriManager *handle = new AriManager(); + Timer::getInstance()->reset(); return handle; } @@ -25,7 +26,10 @@ AriManager *h = (AriManager*)handle; TResult *t_result = (TResult*)malloc(sizeof(TResult)); init_TResult(t_result); + + Timer::getInstance()->out("eveTime before yolo"); h->single_SDK(chan, img, *t_result); + Timer::getInstance()->out("eveTime runing yolo"); return t_result; } diff --git a/src/utils/time_util.cpp b/src/utils/time_util.cpp new file mode 100644 index 0000000..6adc215 --- /dev/null +++ b/src/utils/time_util.cpp @@ -0,0 +1,134 @@ +// +// Created by Scheaven on 2020/5/14. +// + +#include "time_util.h" + +#include <stdio.h> +#include <memory.h> +#include <iostream> +#include <ctime> +#include <string> +#include "stdlib.h" +#include <sys/time.h> +#include <time.h> +#include <unistd.h> + +using namespace std; + +int get_run_time(struct timeval *result, struct timeval *begin, struct timeval * end) +{ + if(begin->tv_sec > end->tv_sec) return -1; + if((begin->tv_sec == end->tv_sec) && (begin->tv_usec > end->tv_usec)) return -2; + result->tv_sec = end->tv_sec - begin->tv_sec; + result->tv_usec = end->tv_usec - begin->tv_usec; + + if(result->tv_usec<0) + { + result->tv_sec--; + result->tv_usec += 1000000; + } + return 0; +} + +time_t strTime2unix(char* timeStamp) +{ + struct tm tm; + memset(&tm, 0, sizeof(tm)); + + sscanf(timeStamp, "%d-%d-%d %d:%d:%d", + &tm.tm_year, &tm.tm_mon, &tm.tm_mday, + &tm.tm_hour, &tm.tm_min, &tm.tm_sec); + + tm.tm_year -= 1900; + tm.tm_mon--; + + return mktime(&tm); +} + +long char_2_unix(char* str1) +{ + char str[100]; +// std::cout<< "==char_2_unix=%s锛�" << str1<< std::endl; + + strcpy(str, str1); + const char *mySplit = "."; + char *p; + char *p1; + char *p2; + + p1 = strtok(str, mySplit); + p2 = strtok(NULL, mySplit); +// std::cout<< "--p1=锛�" << p1 << " : " << p2 << std::endl; + time_t t = strTime2unix(p1); + +// std::cout<< "char_2_unix=t=%s锛�" << t << std::endl; + return t*1000+atoi(p2); +} + + +char* fa_getSysTime() +{ + struct timeval tv; + gettimeofday(&tv,NULL); + struct tm* pTime; + pTime = localtime(&tv.tv_sec); + + static char sTemp[30] = "0"; + snprintf(sTemp, sizeof(sTemp), "%04d-%02d-%02d %02d:%02d:%02d:%03d", pTime->tm_year+1900, \ + pTime->tm_mon+1, pTime->tm_mday, pTime->tm_hour, pTime->tm_min, pTime->tm_sec, \ + tv.tv_usec/1000); + char* TP = sTemp; + return TP; +} + +string random_int(size_t length) +{ + long seed = (unsigned)time(NULL) + (unsigned)clock(); + srand(seed); + auto randchar= []() ->char + { + const char charset[] = "0123456789"; + const size_t max_index = (sizeof(charset) - 1); + return charset[rand()%max_index]; + }; + std::string str(length,0); + std::generate_n(str.begin(),length,randchar); + //SLOG::getInstance()->addLog(0, str + "------seed and id--init-----id:" + to_string(seed)); + return str; +} + + +Timer* Timer::instance = NULL; +Timer* Timer::getInstance() +{ + if (instance==NULL) + { + instance = new Timer(); + } + return instance; +} + +Timer::Timer():beg_(clock_::now()){} +void Timer::reset() +{ + beg_ = clock_::now(); +} + +double Timer::elapsed() const +{ + return std::chrono::duration_cast<second_>(clock_::now() - beg_).count(); +} + +void Timer::out(std::string message) +{ + double t = elapsed(); + // std::cout << message << " elasped time:" << t << "ms" << std::endl; + DEBUG((boost::format("%e: %f ms")%message %t).str()); + reset(); +} + +double Timer::get_duration() const +{ + return elapsed(); +} diff --git a/src/utils/time_util.h b/src/utils/time_util.h new file mode 100644 index 0000000..79b9c68 --- /dev/null +++ b/src/utils/time_util.h @@ -0,0 +1,49 @@ +// +// Created by Scheaven on 2020/5/14. +// + +#ifndef DEMO_TIME_UTIL_H +#define DEMO_TIME_UTIL_H + +#include <sys/time.h> +#include <cstdio> +#include <time.h> +#include <stdlib.h> +#include <string> +#include <algorithm> +#include <sstream> + +int get_run_time(struct timeval *result, struct timeval *begin, struct timeval *end); + +time_t strTime2unix(char* timeStamp); + +long char_2_unix(char* str1); + +char* fa_getSysTime(); + +std::string random_int(size_t length); + +#include <chrono> +#include "log_util.h" +#include <boost/format.hpp> + +class Timer +{ + public: + static Timer* instance; + static Timer* getInstance(); + + Timer(); + public: + void reset(); + double elapsed() const; + void out(std::string message=""); + // DEBUG((boost::format("nhao%d")%1).str()); + double get_duration() const; + private: + using clock_ = std::chrono::high_resolution_clock; + using second_=std::chrono::duration<double,std::milli>; + std::chrono::time_point<clock_>beg_; +}; + +#endif //DEMO_TIME_UTIL_H diff --git a/src/utils/timer_utils.hpp b/src/utils/timer_utils.hpp deleted file mode 100644 index dafccbb..0000000 --- a/src/utils/timer_utils.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include <istream> -#include <string> -#include <chrono> -#include "log_util.h" -#include <boost/format.hpp> - -class Timer -{ - public: - Timer():beg_(clock_::now()){} - void reset() - { - beg_ = clock_::now(); - } - double elapsed() const - { - return std::chrono::duration_cast<second_>(clock_::now() - beg_).count(); - } - void out(std::string message="") - { - double t = elapsed(); - // std::cout << message << " elasped time:" << t << "ms" << std::endl; - DEBUG((boost::format("%e lasped time:%f ms")%message %t).str()); - reset(); - } - // DEBUG((boost::format("nhao%d")%1).str()); - - double get_duration() const - { - return elapsed(); - } - private: - using clock_ = std::chrono::high_resolution_clock; - using second_=std::chrono::duration<double,std::milli>; - std::chrono::time_point<clock_>beg_; -}; -- Gitblit v1.8.0