.NET 程序启动调试器 .NET 测试代码耗费时间

有些场景的.NET程序,不容易设置断点,可以用下面的方法,在.NET代码中增加启动调试器的代码:

if (!Debugger.IsAttached)
Debugger.Launch();

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

在文件开头添加引用using System.Diagnostics;

 

参考下面的代码例子,Visual Studio 2015内置查看两个段点之间代码执行的性能(内存耗费和处理器时间)

class Program
{
static void Main()
{
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch(); // Begin timing
stopwatch.Start(); // Do something 这里是需要测试的耗费时间的代码
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(1);
} // Stop timing
stopwatch.Stop(); // Write result
Console.WriteLine("Time elapsed: {0}",stopwatch.Elapsed);
}
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上一篇:F5安全能力的四大关键词:东西流量精分、DevOps可视化、机器学习、API接口管理


下一篇:Visual Studio 2012 Update 4 RC 启动调试失败解决方案