Skip to content

Commit

Permalink
20190919-c-sharp-calculate-execution-time
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienFremont committed Sep 19, 2017
1 parent 8508ff4 commit 38b86cd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 20190919-c-sharp-calculate-execution-time/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
C# (sharp) – Calculate Execution Time
======

![alt text](screenshots/170919162538937.png)

Stopwatch is designed for this purpose, and is one of the best way to measure time execution in .NET.



## Examples

Here is a simple usecase.

```
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
```

Do not use DateTimes to measure time execution in .NET.

“It is worth mentioning that DateTime.Now often is quite a bit slower than DateTime.UtcNow due to the work that has to be done with timezones, DST and such.

DateTime.UtcNow typically has a resolution of 15 ms. See John Chapman’s blog post about DateTime.Now precision for a great summary.”

You can also check multiples times before you stop the watcher.

```
long step1ElapsedMilliseconds;
long step2ElapsedMilliseconds;
var watch = System.Diagnostics.Stopwatch.StartNew();
// the code that you want to measure comes here 1/2
step1ElapsedMilliseconds = watch.ElapsedMilliseconds;
// the code that you want to measure comes here 2/2
step2ElapsedMilliseconds = watch.ElapsedMilliseconds;
watch.Stop();
Debug.Log("Execution time for step 1 in m: "
+ step1ElapsedMilliseconds );
Debug.Log("Execution time for step 2 in m: "
+ (step2ElapsedMilliseconds - step1ElapsedMilliseconds) );
Debug.Log("Total Execution time in ms: "
+ watch.ElapsedMilliseconds);
```

## Source

[https://github.com/DamienFremont/blog/tree/master/20170919-c-sharp-calculate-execution-time](https://github.com/DamienFremont/blog/tree/master/20170919-c-sharp-calculate-execution-time)
https://github.com/DamienFremont/blog/tree/master/20170919-c-sharp-calculate-execution-time

## References

[https://stackoverflow.com/questions/14019510/calculate-the-execution-time-of-a-method](https://stackoverflow.com/questions/14019510/calculate-the-execution-time-of-a-method)
https://stackoverflow.com/questions/14019510/calculate-the-execution-time-of-a-method


## Origin
[https://damienfremont.com/2017/09/19/c-sharp-calculate-execution-time/](https://damienfremont.com/2017/09/19/c-sharp-calculate-execution-time/)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 38b86cd

Please sign in to comment.