我一直认为 .pdb 文件仅供调试器使用。如果运行时总是检查它们的调试信息,那应该意味着抛出异常时执行速度较慢,因为它必须读取 .pdb,对吧?
所以我做了一个快速测试:
using System;
using System.Text;
namespace PdbSpeedTest
{
class Program
{
static void Main(string[] args)
{
DateTime start = DateTime.Now;
try
{
Program p = new Program();
p.Looper(0);
}
catch (NotImplementedException e)
{
Console.WriteLine(e.StackTrace);
}
TimeSpan span = DateTime.Now - start;
Console.WriteLine(span.TotalMilliseconds.ToString());
}
internal void Looper(int x)
{
try
{
if (x < 100)
Looper(x + 1);
else
throw new NotImplementedException("blah!");
}
catch (NotImplementedException e)
{
throw new NotImplementedException("blah!", e);
}
}
}
}
这只是递归 100 级深度并引发异常。现在查看运行时结果:
作为debug构建with在同一文件夹中的.pdb:
C:\Work\PdbSpeedTest\bin\Debug>PdbSpeedTest.exe
at PdbSpeedTest.Program.Looper(Int32 x) in C:\Work\PdbSpeedTest\Program.cs:line 37
at PdbSpeedTest.Program.Main(String[] args) in C:\Work\PdbSpeedTest\Program.cs:line 16
31.2504
作为debug构建运行没有 .pdb:
C:\Work\PdbSpeedTest\bin\Debug>PdbSpeedTest.exe
at PdbSpeedTest.Program.Looper(Int32 x)
at PdbSpeedTest.Program.Main(String[] args)
15.6252
作为release构建使用 .pdb运行:
C:\Work\PdbSpeedTest\bin\Release>PdbSpeedTest.exe
at PdbSpeedTest.Program.Looper(Int32 x) in C:\Work\PdbSpeedTest\Program.cs:line 37
at PdbSpeedTest.Program.Main(String[] args) in C:\Work\PdbSpeedTest\Program.cs:line 16
31.2504
作为release构建运行没有 .pdb:
C:\Work\PdbSpeedTest\bin\Release>PdbSpeedTest.exe
at PdbSpeedTest.Program.Looper(Int32 x)
at PdbSpeedTest.Program.Main(String[] args)
15.6252
这些是从常规的旧命令提示符运行的,而不是在 Visual Studio 中运行的。所以 .pdb 肯定会添加堆栈跟踪信息,并减慢异常处理速度。
很有趣!