派生自 Algorithm/baseDetector

sunty
2022-03-21 d0a24896f95b4e060011852f80048ebfb0bf5f55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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();
}