PowerToys/Wox.Infrastructure/Stopwatch.cs

45 lines
1.1 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
2015-11-02 21:43:09 +08:00
using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure
{
public static class Stopwatch
{
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
public static void Debug(string name, Action action)
{
#if DEBUG
Normal(name, action);
#else
action();
#endif
}
[Conditional("DEBUG")]
private static void WriteTimeInfo(string name, long milliseconds)
2015-11-02 08:04:05 +08:00
{
string info = $"{name} : {milliseconds}ms";
System.Diagnostics.Debug.WriteLine(info);
Log.Info(info);
2015-11-02 08:04:05 +08:00
}
/// <summary>
/// This stopwatch will also appear only in Debug mode
/// </summary>
public static long Normal(string name, Action action)
{
var stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
WriteTimeInfo(name, milliseconds);
return milliseconds;
}
}
}