-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPerf.Interlocked.cs
69 lines (53 loc) · 2.24 KB
/
Perf.Interlocked.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Threading.Tests
{
[BenchmarkCategory(Categories.Libraries, Categories.Runtime)]
public class Perf_Interlocked
{
private int _int = 0;
private long _long = 0;
private string _location, _newValue, _comparand;
[Benchmark]
public int Increment_int() => Interlocked.Increment(ref _int);
[Benchmark]
public int Decrement_int() => Interlocked.Decrement(ref _int);
[Benchmark]
public long Increment_long() => Interlocked.Increment(ref _long);
[Benchmark]
public long Decrement_long() => Interlocked.Decrement(ref _long);
[Benchmark]
public int Add_int() => Interlocked.Add(ref _int, 2);
[Benchmark]
public long Add_long() => Interlocked.Add(ref _long, 2);
[Benchmark]
public int Exchange_int() => Interlocked.Exchange(ref _int, 1);
[Benchmark]
public long Exchange_long() => Interlocked.Exchange(ref _long, 1);
[Benchmark]
public int CompareExchange_int() => Interlocked.CompareExchange(ref _int, 1, 0);
[Benchmark]
public long CompareExchange_long() => Interlocked.CompareExchange(ref _long, 1, 0);
[GlobalSetup(Target = nameof(CompareExchange_object_Match))]
public void Setup_CompareExchange_object_Match()
{
_location = "What?";
_newValue = "World";
_comparand = "What?";
}
[Benchmark]
public string CompareExchange_object_Match() => Interlocked.CompareExchange(ref _location, _newValue, _comparand);
[GlobalSetup(Target = nameof(CompareExchange_object_NoMatch))]
public void Setup_CompareExchange_object_NoMatch()
{
_location = "Hello";
_newValue = "World";
_comparand = "What?";
}
[Benchmark]
public string CompareExchange_object_NoMatch() => Interlocked.CompareExchange(ref _location, _newValue, _comparand);
}
}