Skip to main content
fixed code issues (incorrect variable name, extra space)
Source Link
ProgrammingLlama
  • 39.4k
  • 7
  • 79
  • 105

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); 

A more reliable version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; Ii < repeat; i ++i++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); 

A more reliable version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; I < repeat; i ++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); 

A more reliable version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < repeat; i++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop();   Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); 

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

A bettermore reliable version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; I < repeat; i ++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); 

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

A better version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; I < repeat; i ++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop();   Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); 

A more reliable version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; I < repeat; i ++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.

Source Link

This is not professional enough:

Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); 

Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);

A better version is:

PerformWork(); int repeat = 1000; Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; I < repeat; i ++) { PerformWork(); } sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat); 

In my real code, I will add GC.Collect call to change managed heap to a known state, and add Sleep call so that different intervals of code can be easily separated in ETW profile.