Skip to content

Instantly share code, notes, and snippets.

@chenyahui
Created August 8, 2018 09:26
Show Gist options
  • Save chenyahui/092eb7e89dc32c45d0d895a973b4a1ab to your computer and use it in GitHub Desktop.
Save chenyahui/092eb7e89dc32c45d0d895a973b4a1ab to your computer and use it in GitHub Desktop.
一个通用的C#时耗统计函数
class NetTimeException : Exception
{
public string msg;
public long time;
public NetTimeException(string msg, long time)
{
this.msg = msg;
this.time = time;
}
}
static long TimeMethodWrapper(Action action)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
try
{
action.Invoke();
}
catch (Exception e)
{
stopwatch.Stop();
throw new NetTimeException(e.Message, stopwatch.ElapsedMilliseconds);
}
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment