How can I profile C# methods per second?

The System.Diagnostics.Stopwatch class will help you here, but be careful to use the results somehow so that the optimizer doesn't eliminate the logic you're trying to measure.

Beyond that, just run the code you're profiling several million times in a loop (adjust the iteration count to make it take between 1 and 30 seconds), then divide the number of iterations by the time taken to get the throughput in executions per second.


What I would do:

  • Start a Stopwatch.
  • In those functions, I increment a simple variable (long, float, or double, depending on how often you think they'll get called) so it's incremented on each call.
  • Call the first function.
  • Stop the Stopwatch and check the TotalSeconds against the variable I've been incrementing.
  • Repeat for the second function.