1
- using System . Collections . Generic ;
2
1
using UnityEngine ;
3
2
4
3
namespace MLAPI . Profiling
5
4
{
6
- public struct Sample
7
- {
8
- public int Count ;
9
- public float TimeRecorded ;
10
- }
11
-
12
5
public class ProfilerStat
13
6
{
14
7
public ProfilerStat ( string name )
@@ -18,67 +11,28 @@ public ProfilerStat(string name)
18
11
}
19
12
20
13
public string PrettyPrintName ;
21
- protected int m_MaxSamples = 10 ;
22
-
23
- protected LinkedList < Sample > m_Data = new LinkedList < Sample > ( ) ;
24
-
25
- private bool m_IsDirty = true ;
26
14
27
- protected float m_LastCount ;
28
- protected float m_LastTime ;
15
+ private int m_CurrentSecond = 0 ;
16
+ private int m_TotalAmount = 0 ;
17
+ private int m_Sample = 0 ;
29
18
30
19
public virtual void Record ( int amt = 1 )
31
20
{
32
- m_IsDirty = true ;
33
- var timeNow = Time . time ;
34
- // 'Record' can get called many times in the same frame (for the same exact timestamp)
35
- // This not only blows out the samples but makes the rate computation break since we
36
- // have n samples with a time delta of zero.
37
- //
38
- // Instead, if we want to record a value at the same exact time as our last
39
- // sample, just adjust that sample
40
- if ( m_Data . First != null && m_Data . First . Value . TimeRecorded == timeNow )
21
+ int currentSecond = ( int ) Time . unscaledTime ;
22
+ if ( currentSecond != m_CurrentSecond )
41
23
{
42
- m_Data . First . Value = new Sample ( )
43
- {
44
- Count = m_Data . First . Value . Count + amt ,
45
- TimeRecorded = m_Data . First . Value . TimeRecorded
46
- } ;
47
- }
48
- else
49
- {
50
- m_Data . AddFirst ( new Sample ( ) { Count = amt , TimeRecorded = Time . time } ) ;
51
- while ( m_Data . Count > m_MaxSamples )
52
- {
53
- m_Data . RemoveLast ( ) ;
54
- }
24
+ m_Sample = m_TotalAmount ;
25
+
26
+ m_TotalAmount = 0 ;
27
+ m_CurrentSecond = currentSecond ;
55
28
}
29
+
30
+ m_TotalAmount += amt ;
56
31
}
57
32
58
33
public virtual float SampleRate ( )
59
34
{
60
- if ( m_IsDirty )
61
- {
62
- LinkedListNode < Sample > node = m_Data . First ;
63
- m_LastCount = 0 ;
64
- m_LastTime = m_Data . Last ? . Value . TimeRecorded ?? 0.0f ;
65
-
66
- while ( node != null )
67
- {
68
- m_LastCount += node . Value . Count ;
69
- node = node . Next ;
70
- }
71
-
72
- m_IsDirty = false ;
73
- }
74
-
75
- float delta = Time . time - m_LastTime ;
76
- if ( delta == 0.0f )
77
- {
78
- return 0.0f ;
79
- }
80
-
81
- return m_LastCount / delta ;
35
+ return m_Sample ;
82
36
}
83
37
}
84
38
@@ -90,12 +44,6 @@ public ProfilerIncStat(string name) : base(name) { }
90
44
91
45
public override void Record ( int amt = 1 )
92
46
{
93
- m_Data . AddFirst ( new Sample ( ) { Count = amt , TimeRecorded = Time . time } ) ;
94
- while ( m_Data . Count > m_MaxSamples )
95
- {
96
- m_Data . RemoveLast ( ) ;
97
- }
98
-
99
47
m_InternalValue += amt ;
100
48
}
101
49
0 commit comments