1 double re;
2 long ticks;
3
4 Stopwatch sw = Stopwatch.StartNew();
5 for (int i = 0; i < 1000; i++)
6 {
7 try
8 {
9 re = double.Parse("123");
10 }
11 catch (Exception)
12 {
13 re = 0;
14 }
15 }
16
17 sw.Stop();
18 ticks = sw.ElapsedTicks;
19 Console.WriteLine("double.parse()成功,{0} ticks", ticks);
20
21 sw = Stopwatch.StartNew();
22 for (int i = 0; i < 1000; i++)
23 {
24 if (!double.TryParse("123", out re))
25 {
26 re = 0;
27 }
28 }
29
30 sw.Stop();
31 ticks = sw.ElapsedTicks;
32 Console.WriteLine("double.TryParse()成功,{0} ticks", ticks);
33
34
35
36 sw = Stopwatch.StartNew();
37 for (int i = 0; i < 1000; i++)
38 {
39 try
40 {
41 re = double.Parse("aaa");
42 }
43 catch (Exception)
44 {
45 re = 0;
46 }
47 }
48
49 sw.Stop();
50 ticks = sw.ElapsedTicks;
51 Console.WriteLine("double.parse()失败,{0} ticks", ticks);
52
53 sw = Stopwatch.StartNew();
54 for (int i = 0; i < 1000; i++)
55 {
56 if (!double.TryParse("aaa", out re))
57 {
58 re = 0;
59 }
60 }
61
62 sw.Stop();
63 ticks = sw.ElapsedTicks;
64 Console.WriteLine("double.TryParse()失败,{0} ticks", ticks);
结果: