Repository navigation
Expand file tree
/
Copy pathCast.cs
More file actions
168 lines (148 loc) · 4.91 KB
/
Copy pathCast.cs
File metadata and controls
168 lines (148 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
namespace NumSharp.Benchmark
{
//BenchmarkDotNet=v0.11.5, OS=Windows 10.0.17763.557 (1809/October2018Update/Redstone5)
//Intel Core i7-6700K CPU 4.00GHz(Skylake), 1 CPU, 8 logical and 4 physical cores
//.NET Core SDK = 3.0.100-preview-010184
//
//[Host] : .NET Core 2.1.8 (CoreCLR 4.6.27317.03, CoreFX 4.6.27317.03), 64bit RyuJIT
//Job-CBCCQS : .NET Core 2.1.8 (CoreCLR 4.6.27317.03, CoreFX 4.6.27317.03), 64bit RyuJIT
//
//IterationCount=10 LaunchCount=1 WarmupCount=3
//
//| Method | Mean | Error | StdDev | Min | Max | Median | Ratio | RatioSD |
//|----------- |-------------:|-----------:|-----------:|-------------:|-------------:|-------------:|-------:|--------:|
//| As | 94.378 us | 3.7468 us | 2.4783 us | 91.947 us | 98.740 us | 93.336 us | 33.47 | 2.23 |
//| MakeRef1 | 1,626.859 us | 27.6512 us | 16.4548 us | 1,610.047 us | 1,658.922 us | 1,620.051 us | 575.01 | 29.54 |
//| MakeRef2 | 156.498 us | 5.5628 us | 3.3103 us | 152.471 us | 162.297 us | 156.659 us | 55.29 | 2.55 |
//| ClassicIs | 120.593 us | 4.3033 us | 2.5608 us | 117.739 us | 125.350 us | 119.360 us | 42.63 | 2.54 |
//| ModernIs | 100.975 us | 5.2199 us | 3.4526 us | 95.176 us | 105.901 us | 102.073 us | 35.59 | 1.84 |
//| DirectCast | 7.990 us | 0.1783 us | 0.1061 us | 7.883 us | 8.235 us | 7.974 us | 2.83 | 0.17 |
//| Baseline | 2.837 us | 0.2768 us | 0.1647 us | 2.638 us | 3.159 us | 2.790 us | 1.00 | 0.00 |
[SimpleJob(launchCount: 1, warmupCount: 3, iterationCount: 10)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
[HtmlExporter]
public class Cast
{
private object input = "woof";
private string input2 = "woof";
[Benchmark]
public int As()
{
for (int i = 0; i < 100_000; i++)
{
string text = input as string;
if (text != null)
{
var txt = text.Length;
}
}
return 0;
}
[Benchmark]
public int MakeRef1()
{
for (int i = 0; i < 100_000; i++)
{
var txt = ReinterpretCast<object, string>(input);
}
return 0;
}
[MethodImpl(Optimize)]
static unsafe TDest ReinterpretCast<TSource, TDest>(TSource source)
{
var tr = __makeref(source);
TDest w = default(TDest);
var trw = __makeref(w);
*((IntPtr*)&trw) = *((IntPtr*)&tr);
return __refvalue(trw, TDest);
}
[Benchmark]
public unsafe int MakeRef2()
{
for (int i = 0; i < 100_000; i++)
{
var x = __makeref(input);
var txt = Unsafe.AsRef<string>((void*)*(IntPtr*)*(IntPtr*)&x);
}
return 0;
}
[Benchmark]
public int ClassicIs()
{
for (int i = 0; i < 100_000; i++)
{
if (input is string)
{
var txt = ((string)input).Length;
}
}
return 0;
}
[Benchmark]
public int ModernIs()
{
for (int i = 0; i < 100_000; i++)
{
if (input is string text)
{
var txt = text.Length;
}
}
return 0;
}
[Benchmark]
public int DirectCast()
{
for (int i = 0; i < 100_00; i++)
{
var txt = ((string)input).Length;
}
return 0;
}
[Benchmark]
public int DirectCastTryCatch()
{
for (int i = 0; i < 100_00; i++)
{
try
{
var txt = ((string)input).Length;
}
catch { }
}
return 0;
}
[Benchmark]
public int DirectCastTryCatch_Throws()
{
for (int i = 0; i < 100_00; i++)
{
try
{
var txt = ((float)input);
}
catch { }
}
return 0;
}
[Benchmark(Baseline = true)]
public int Baseline()
{
for (int i = 0; i < 100_00; i++)
{
if (input2 != null)
{
var txt = input2.Length;
}
}
return 0;
}
}
}