How do I log time consuming operations? (C#)
-
02-06-2021 - |
Question
My game runs a lot of calculations, sometimes the framerate drops without obvious reasons, and I'd like to find out what takes so long to process. How to create a timer (or something) to print out elapsed times for each major operation?
UPD: after running the default profiler, I found out that the bottleneck is in my tile array loop. I have about half million tiles and the loop asks for tile's one property, for about 200 tiles per frame. Like so:
if (tileArray[tile].this_tile_is_on_screen)
tileArray[tile].Draw();
Could this lead to slowdowns when the tileArray is very big? Any advice on how to avoid it?
Solution
Use a profiler such as the EQATEC Profiler for .NET, or the one built into the Premium and Ultimate versions of Visual Studio.
If you are already using a logging framework, such as log4Net, you can sprinkle sections of code with logging and turn up the logging verbosity.
OTHER TIPS
I usually use Stopwatch class from Diagnostic namespace for timer.
And WriteLine function from Debug class for print out to print out elapsed times.