-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathCounting.h
77 lines (67 loc) · 2.05 KB
/
Counting.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
#ifndef __Counting_H__
#define __Counting_H__
#include <iostream>
#include <unordered_map>
#include "SPlisHSPlasH/Common.h"
#include "Logger.h"
#include "Timing.h"
namespace Utilities
{
#define INCREASE_COUNTER(counterName, increaseBy) \
Utilities::Counting::increaseCounter(counterName, increaseBy);
#define INIT_COUNTING \
std::unordered_map<std::string, Utilities::AverageCount> Utilities::Counting::m_averageCounts;
struct AverageCount
{
Real sum;
unsigned int numberOfCalls;
};
class Counting
{
public:
static std::unordered_map<std::string, AverageCount> m_averageCounts;
static void reset()
{
m_averageCounts.clear();
}
FORCE_INLINE static void increaseCounter(const std::string& name, const Real increaseBy)
{
std::unordered_map<std::string, AverageCount>::iterator iter;
iter = Counting::m_averageCounts.find(name);
if (iter != Counting::m_averageCounts.end())
{
iter->second.sum += increaseBy;
iter->second.numberOfCalls++;
}
else
{
AverageCount ac;
ac.sum = increaseBy;
ac.numberOfCalls = 1;
Counting::m_averageCounts[name] = ac;
}
}
FORCE_INLINE static void printAverageCounts()
{
std::unordered_map <std::string, AverageCount>::iterator iter;
for (iter = Counting::m_averageCounts.begin(); iter != Counting::m_averageCounts.end(); iter++)
{
AverageCount &ac = iter->second;
const double avgCount = ac.sum / ac.numberOfCalls;
LOG_INFO << "Average number: " << iter->first.c_str() << ": " << avgCount;
}
LOG_INFO << "---------------------------------------------------------------------------\n";
}
FORCE_INLINE static void printCounterSums()
{
std::unordered_map <std::string, AverageCount>::iterator iter;
for (iter = Counting::m_averageCounts.begin(); iter != Counting::m_averageCounts.end(); iter++)
{
AverageCount &ac = iter->second;
LOG_INFO << "Total number: " << iter->first.c_str() << ": " << ac.sum;
}
LOG_INFO << "---------------------------------------------------------------------------\n";
}
};
}
#endif // __Counting_H__