This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
operation_cpu_timer.h
executable file
·83 lines (74 loc) · 1.77 KB
/
operation_cpu_timer.h
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
#ifndef ANDROID_HARDWARE_NEURALNETWORKS_V1_2_OPERATION_CPU_TIMER_H
#define ANDROID_HARDWARE_NEURALNETWORKS_V1_2_OPERATION_CPU_TIMER_H
#include <vector>
#include <string>
NAME_SPACE_BEGIN
class OperationCpuTimer
{
public:
OperationCpuTimer() : avg(0), opIndex(0) {}
~OperationCpuTimer() {}
void add(long diff) { times.push_back(diff); }
void set(uint32_t index, const std::string& name)
{
opIndex = index;
opName = name;
size_t s = opName.length();
while (s < 20)
{
s++;
opName += " ";
}
}
long getAvg()
{
if (times.size() == 0)
{
avg = 0;
}
else if (times.size() == 1)
{
avg = times[0];
}
else
{
long sum = 0;
for (size_t i = 1; i < times.size(); ++i)
{
sum += times[i];
}
avg = sum / (times.size()-1);
}
return avg;
}
long show(long acc, float sum)
{
NN_GPU_PERF("op %02d (%s): \t%s per run (%.2f%%, %.2f%%)",
opIndex, opName.c_str(),
getTimeString(avg).c_str(),
avg*100/sum,(avg+acc)*100/sum);
return avg + acc;
}
static bool sort(const OperationCpuTimer& lhs, const OperationCpuTimer& rhs)
{
return (lhs.avg > rhs.avg);
}
private:
static std::string getTimeString(long t)
{
if (t > 1000000)
{
return std::to_string(t/1000000.) + " s";
}
else
{
return std::to_string(t/1000.) + " ms";
}
}
std::vector<long> times; //holds time in us (1e-6 second)
long avg;
uint32_t opIndex;
std::string opName;
};
NAME_SPACE_STOP
#endif