FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
NetCode/NetCode.Benchmarks/ByteWriterBenchmark.cs at main · Levchenkov/NetCode · GitHub
Levchenkov
/
NetCode
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
71
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
NetCode
/
NetCode.Benchmarks
/
ByteWriterBenchmark.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
111 lines (90 loc) · 2.79 KB
Breadcrumbs
NetCode
/
NetCode.Benchmarks
/
ByteWriterBenchmark.cs
Copy path
File metadata and controls
111 lines (90 loc) · 2.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
BenchmarkDotNet
.
Attributes
;
namespace
NetCode
.
Benchmarks
;
/// <summary>
/// BenchmarkDotNet=v0.13.1, OS=macOS Big Sur 11.5.2 (20G95) [Darwin 20.6.0]
/// Intel Core i7-9750H CPU 2.60GHz, 1 CPU, 12 logical and 6 physical cores
/// .NET SDK=6.0.100
/// [Host] : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
/// DefaultJob : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
///
/// | Method | Mean | Error | StdDev | Ratio | RatioSD | Allocated |
/// |----------------- |-----------:|---------:|---------:|------:|--------:|----------:|
/// | BinaryPrimitives | 325.7 ns | 1.39 ns | 1.30 ns | 1.00 | 0.00 | - |
/// | ByteWriter | 330.3 ns | 1.67 ns | 1.48 ns | 1.01 | 0.01 | - |
/// | BitWriter | 338.0 ns | 1.69 ns | 1.58 ns | 1.04 | 0.00 | - |
/// | BinaryWriter | 2,344.7 ns | 12.08 ns | 10.71 ns | 7.20 | 0.03 | - |
///
/// </summary>
[
MemoryDiagnoser
]
public
class
ByteWriterBenchmark
{
private
ByteWriter
_byteWriter
;
private
BitWriter
_bitWriter
;
private
byte
[
]
_array
;
private
BinaryWriter
_binaryWriter
;
[
GlobalSetup
]
public
void
GlobalSetup
(
)
{
_array
=
new
byte
[
2000
]
;
_byteWriter
=
new
ByteWriter
(
_array
)
;
_bitWriter
=
new
BitWriter
(
_array
)
;
_binaryWriter
=
new
BinaryWriter
(
new
MemoryStream
(
_array
)
)
;
}
[
Benchmark
(
Baseline
=
true
)
]
public
int
BinaryPrimitives
(
)
{
var
count
=
0
;
Span
<
byte
>
span
=
_array
.
AsSpan
(
)
;
for
(
int
i
=
0
;
i
<
255
;
i
++
)
{
System
.
Buffers
.
Binary
.
BinaryPrimitives
.
WriteInt32LittleEndian
(
span
,
i
)
;
span
=
span
.
Slice
(
4
)
;
count
+=
4
;
}
return
count
;
}
[
Benchmark
]
public
byte
[
]
Shift
(
)
{
var
index
=
0
;
for
(
int
i
=
0
;
i
<
255
;
i
++
)
{
_array
[
index
++
]
=
(
byte
)
(
i
)
;
_array
[
index
++
]
=
(
byte
)
(
i
>>
8
)
;
_array
[
index
++
]
=
(
byte
)
(
i
>>
16
)
;
_array
[
index
++
]
=
(
byte
)
(
i
>>
24
)
;
}
return
_array
;
}
[
Benchmark
]
public
int
ByteWriter
(
)
{
_byteWriter
.
Clear
(
)
;
for
(
int
i
=
0
;
i
<
255
;
i
++
)
{
_byteWriter
.
Write
(
i
)
;
}
return
_byteWriter
.
Count
;
}
[
Benchmark
]
public
int
BitWriter
(
)
{
_bitWriter
.
Clear
(
)
;
for
(
int
i
=
0
;
i
<
255
;
i
++
)
{
_bitWriter
.
Write
(
i
)
;
}
return
_bitWriter
.
BytesCount
;
}
[
Benchmark
]
public
long
BinaryWriter
(
)
{
_binaryWriter
.
Seek
(
0
,
SeekOrigin
.
Begin
)
;
for
(
int
i
=
0
;
i
<
255
;
i
++
)
{
_binaryWriter
.
Write
(
i
)
;
}
_binaryWriter
.
Flush
(
)
;
return
_binaryWriter
.
BaseStream
.
Position
;
}
}
Back
|
FazBrowse Home
|
New Git URL