Replace Dispose with Lambda

1. Faster
2. Fix #361
This commit is contained in:
bao-qian
2015-11-04 21:35:04 +00:00
parent 57a06aa122
commit df0f310ddd
6 changed files with 67 additions and 57 deletions

View File

@@ -4,35 +4,41 @@ using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure
{
public class Timeit : IDisposable
public static class Timeit
{
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly string _name;
public Timeit(string name)
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
public static void StopwatchDebug(string name, Action action)
{
_name = name;
_stopwatch.Start();
#if DEBUG
Stopwatch(name, action);
#else
action();
#endif
}
public long Current
[Conditional("DEBUG")]
private static void WriteTimeInfo(string name, long milliseconds)
{
get
{
_stopwatch.Stop();
long seconds = _stopwatch.ElapsedMilliseconds;
_stopwatch.Start();
return seconds;
}
}
public void Dispose()
{
_stopwatch.Stop();
string info = _name + " : " + _stopwatch.ElapsedMilliseconds + "ms";
string info = $"{name} : {milliseconds}ms";
Debug.WriteLine(info);
Log.Info(info);
}
/// <summary>
/// This stopwatch will also appear only in Debug mode
/// </summary>
public static long Stopwatch(string name, Action action)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
WriteTimeInfo(name, milliseconds);
return milliseconds;
}
}
}