StopwatchHelper.cs 732 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. namespace Common.Stopwatch
  3. {
  4. /// <summary>
  5. /// 计算某一段代码执行时间帮助类
  6. /// </summary>
  7. public static class StopwatchHelper
  8. {
  9. /// <summary>
  10. /// 计算时间
  11. /// </summary>
  12. /// <param name="function">要被执行的代码</param>
  13. /// <returns></returns>
  14. public static string Stopwatch(Action function)
  15. {
  16. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  17. sw.Start();
  18. //开始执行业务代码
  19. function();
  20. sw.Stop();
  21. TimeSpan timeSpan = sw.Elapsed;
  22. return (timeSpan.TotalMilliseconds / 1000) + "s";
  23. }
  24. }
  25. }