Skip to content

Instantly share code, notes, and snippets.

@smoogipoo
Last active June 27, 2023 10:11
Show Gist options
  • Save smoogipoo/cb3b61022852bedba86472e36b1e67f7 to your computer and use it in GitHub Desktop.
Save smoogipoo/cb3b61022852bedba86472e36b1e67f7 to your computer and use it in GitHub Desktop.
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
namespace BenchmarksProject
{
public class BenchmarkFalseSharing
{
[Params(1, 4, 8, 16, 32, 64, 128)]
public int Separation { get; set; }
private readonly int[] data = new int[256];
[Benchmark]
public void FalseSharing()
{
Parallel.Invoke(incX, incY);
void incX()
{
for (int i = 0; i < 1_000_000; i++)
data[0]++;
}
void incY()
{
for (int i = 0; i < 1_000_000; i++)
data[Separation]++;
}
}
}
}
@smoogipoo
Copy link
Author

smoogipoo commented Jun 27, 2023

BenchmarkDotNet=v0.12.1, OS=manjaro 
AMD Ryzen 9 3950X, 1 CPU, 32 logical and 16 physical cores
.NET Core SDK=7.0.305
  [Host]     : .NET Core 6.0.16 (CoreCLR 6.0.1623.17311, CoreFX 6.0.1623.17311), X64 RyuJIT
  DefaultJob : .NET Core 6.0.16 (CoreCLR 6.0.1623.17311, CoreFX 6.0.1623.17311), X64 RyuJIT


|       Method | Separation |     Mean |     Error |    StdDev |
|------------- |----------- |---------:|----------:|----------:|
| FalseSharing |          1 | 6.591 ms | 0.1292 ms | 0.1680 ms |
| FalseSharing |          4 | 6.662 ms | 0.1285 ms | 0.1480 ms |
| FalseSharing |          8 | 4.411 ms | 0.0880 ms | 0.1519 ms |
| FalseSharing |         16 | 4.388 ms | 0.0859 ms | 0.1117 ms |
| FalseSharing |         32 | 4.452 ms | 0.0887 ms | 0.2107 ms |
| FalseSharing |         64 | 4.455 ms | 0.0866 ms | 0.1323 ms |
| FalseSharing |        128 | 4.426 ms | 0.0881 ms | 0.1676 ms |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment