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