@@ -28,7 +28,10 @@ internal sealed class BenchmarkCounter
2828 private readonly Vector2 _textPos = new ( 10 , 10 ) ;
2929 private readonly Vector2 _shadowPos = new ( 11 , 11 ) ;
3030
31- public BenchmarkCounter ( Func < int > countResolver , ContentManager contentManager , GraphicsDevice graphicsDevice )
31+ public BenchmarkCounter (
32+ Func < int > countResolver ,
33+ ContentManager contentManager ,
34+ GraphicsDevice graphicsDevice )
3235 {
3336 _countResolver = countResolver ;
3437 _fpsHistory = new int [ 60 ] ;
@@ -50,40 +53,45 @@ public void Draw(GameTime gameTime)
5053
5154 public void Update ( GameTime gameTime )
5255 {
53- var elapsedSeconds = gameTime . ElapsedGameTime . TotalSeconds ;
54- _frameTime = gameTime . ElapsedGameTime . TotalMilliseconds ;
56+ var elapsedGameTime = gameTime . ElapsedGameTime ;
57+ var elapsedSeconds = elapsedGameTime . TotalSeconds ;
58+
59+ _frameTime = elapsedGameTime . TotalMilliseconds ;
5560 _fpsTimer += elapsedSeconds ;
5661
57- if ( _fpsTimer >= 1.0 )
62+ if ( _fpsTimer < 1.0 )
5863 {
59- _fps = _frameCount ;
64+ return ;
65+ }
6066
61- _historySum -= _fpsHistory [ _historyIndex ] ;
62- _fpsHistory [ _historyIndex ] = _fps ;
63- _historySum += _fps ;
67+ _fps = _frameCount ;
6468
65- _historyIndex = ( _historyIndex + 1 ) % 60 ;
66- if ( _historyIndex == 0 ) _historyFull = true ;
69+ ref var historySum = ref _fpsHistory [ _historyIndex ] ;
70+ _historySum -= historySum ;
71+ historySum = _fps ;
72+ _historySum += _fps ;
6773
68- var historyCount = _historyFull ? 60 : _historyIndex ;
69- _avgFps = ( double ) _historySum / historyCount ;
74+ _historyIndex = ( _historyIndex + 1 ) % 60 ;
75+ if ( _historyIndex == 0 ) _historyFull = true ;
7076
71- var alloc = GC . GetTotalMemory ( false ) / 1024.0 / 1024.0 ;
72- var count = _countResolver ( ) ;
77+ var historyCount = _historyFull ? 60 : _historyIndex ;
78+ _avgFps = ( double ) _historySum / historyCount ;
7379
74- // Очищаем буфер и записываем новые данные без создания строк
75- var culture = CultureInfo . InvariantCulture ;
80+ var alloc = GC . GetTotalMemory ( false ) / 1024.0 / 1024.0 ;
81+ var count = _countResolver ( ) ;
7682
77- _stringBuilder . Clear ( ) ;
78- _stringBuilder
79- . Append ( $ "{ _fps } FPS")
80- . Append ( culture , $ " | Avg:{ _avgFps : F1} fps")
81- . Append ( culture , $ " | Entities:{ count : N0} ")
82- . Append ( culture , $ " | Frame time:{ _frameTime : F1} ms")
83- . Append ( culture , $ " | Alloc:{ alloc : F3} mb") ;
83+ // Очищаем буфер и записываем новые данные без создания строк
84+ var culture = CultureInfo . InvariantCulture ;
8485
85- _frameCount = 0 ;
86- _fpsTimer = 0 ;
87- }
86+ _stringBuilder . Clear ( ) ;
87+ _stringBuilder
88+ . Append ( $ "{ _fps } FPS")
89+ . Append ( culture , $ " | Avg:{ _avgFps : F1} fps")
90+ . Append ( culture , $ " | Entities:{ count : N0} ")
91+ . Append ( culture , $ " | Frame time:{ _frameTime : F1} ms")
92+ . Append ( culture , $ " | Alloc:{ alloc : F3} mb") ;
93+
94+ _frameCount = 0 ;
95+ _fpsTimer = 0 ;
8896 }
8997}
0 commit comments