Method Run Time

how to find out how long it takes to take a method in c # for example i have label1 and method

public int MakeSome(int a, int b) { for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { // some operation } } return returnIntValue; } 

Know how to find out how many milliseconds the MakeSome method takes and write the value to label1. thanks

+4
source share
3 answers

Use the Stopwatch class from the System.Diagnostics namespace.

+11
source

You can use the Stopwatch class:

 Stopwatch st = new Stopwatch(); st.Start(); // call MakeSome method... st.Stop(); 

Then you can check the st.ElapsedMilliseconds property.

+14
source

Create a temporary variable and assign DateTime.Now, then subtract twice.

(Use a stopwatch class instead - it's more elegant)

0
source

Source: https://habr.com/ru/post/1299082/


All Articles