Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I profile C# methods per second?

I don't know if the title makes sense, but I am trying to time two different methods and see how many times they execute per second, or say per 10 seconds.

For instance:

DividePolygons1(Polygon[] polys)
DividePolygons2(Polygon[] polys)

DividePolygons1 ran:
1642 times per 1 second

DividePolygons2 ran:
1890 times per 1 second
like image 836
Joan Venge Avatar asked Dec 13 '10 22:12

Joan Venge


People also ask

What is profiling in C?

In software engineering, profiling ("program profiling", "software profiling") is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls.

How do memory profilers work?

Memory ProfilerIt monitors the memory consumption of a Python job process. Also, it performs a line-by-line analysis of the memory consumption of the application. The line-by-line memory usage mode works in the same way as the line_profiler. It decorates the function you would like to profile using @profile function.

What is a Python profiler?

Introduction to the profilers cProfile and profile provide deterministic profiling of Python programs. A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.


2 Answers

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.

like image 58
Ben Voigt Avatar answered Sep 21 '22 02:09

Ben Voigt


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.
like image 31
BeemerGuy Avatar answered Sep 21 '22 02:09

BeemerGuy