diff --git a/samples/BenchmarkDotNet.Samples/Intro/IntroEmpty.cs b/samples/BenchmarkDotNet.Samples/Intro/IntroEmpty.cs new file mode 100644 index 000000000..a9a5f507c --- /dev/null +++ b/samples/BenchmarkDotNet.Samples/Intro/IntroEmpty.cs @@ -0,0 +1,87 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Attributes.Columns; +using BenchmarkDotNet.Attributes.Jobs; + +namespace BenchmarkDotNet.Samples.Intro +{ + [MedianColumn, Q3Column, MaxColumn] + [LegacyJitX64Job, RyuJitX64Job, MonoJob] + [KeepBenchmarkFiles] + public class IntroEmpty + { + [Benchmark] public void Void1() {} + [Benchmark] public void Void2() {} + [Benchmark] public void Void3() {} + [Benchmark] public void Void4() {} + + [Benchmark] public byte Byte1() => 0; + [Benchmark] public byte Byte2() => 0; + [Benchmark] public byte Byte3() => 0; + [Benchmark] public byte Byte4() => 0; + + [Benchmark] public sbyte Sbyte1() => 0; + [Benchmark] public sbyte Sbyte2() => 0; + [Benchmark] public sbyte Sbyte3() => 0; + [Benchmark] public sbyte Sbyte4() => 0; + + [Benchmark] public short Short1() => 0; + [Benchmark] public short Short2() => 0; + [Benchmark] public short Short3() => 0; + [Benchmark] public short Short4() => 0; + + [Benchmark] public ushort Ushort1() => 0; + [Benchmark] public ushort Ushort2() => 0; + [Benchmark] public ushort Ushort3() => 0; + [Benchmark] public ushort Ushort4() => 0; + + [Benchmark] public int Int1() => 0; + [Benchmark] public int Int2() => 0; + [Benchmark] public int Int3() => 0; + [Benchmark] public int Int4() => 0; + + [Benchmark] public uint Uint1() => 0u; + [Benchmark] public uint Uint2() => 0u; + [Benchmark] public uint Uint3() => 0u; + [Benchmark] public uint Uint4() => 0u; + + [Benchmark] public bool Bool1() => false; + [Benchmark] public bool Bool2() => false; + [Benchmark] public bool Bool3() => false; + [Benchmark] public bool Bool4() => false; + + [Benchmark] public char Char1() => 'a'; + [Benchmark] public char Char2() => 'a'; + [Benchmark] public char Char3() => 'a'; + [Benchmark] public char Char4() => 'a'; + + [Benchmark] public float Float1() => 0f; + [Benchmark] public float Float2() => 0f; + [Benchmark] public float Float3() => 0f; + [Benchmark] public float Float4() => 0f; + + [Benchmark] public double Double1() => 0d; + [Benchmark] public double Double2() => 0d; + [Benchmark] public double Double3() => 0d; + [Benchmark] public double Double4() => 0d; + + [Benchmark] public long Long1() => 0L; + [Benchmark] public long Long2() => 0L; + [Benchmark] public long Long3() => 0L; + [Benchmark] public long Long4() => 0L; + + [Benchmark] public ulong Ulong1() => 0uL; + [Benchmark] public ulong Ulong2() => 0uL; + [Benchmark] public ulong Ulong3() => 0uL; + [Benchmark] public ulong Ulong4() => 0uL; + + [Benchmark] public string String1() => ""; + [Benchmark] public string String2() => ""; + [Benchmark] public string String3() => ""; + [Benchmark] public string String4() => ""; + + [Benchmark] public object Object1() => null; + [Benchmark] public object Object2() => null; + [Benchmark] public object Object3() => null; + [Benchmark] public object Object4() => null; + } +} \ No newline at end of file diff --git a/samples/BenchmarkDotNet.Samples/Intro/IntroEmptyMethods.cs b/samples/BenchmarkDotNet.Samples/Intro/IntroEmptyMethods.cs deleted file mode 100644 index e9ff69cea..000000000 --- a/samples/BenchmarkDotNet.Samples/Intro/IntroEmptyMethods.cs +++ /dev/null @@ -1,32 +0,0 @@ -using BenchmarkDotNet.Attributes; - -namespace BenchmarkDotNet.Samples.Intro -{ - public class IntroEmptyMethods - { - [Benchmark] - public void Empty1() - { - } - - [Benchmark] - public void Empty2() - { - } - - [Benchmark] - public void Empty3() - { - } - - [Benchmark] - public void Empty4() - { - } - - [Benchmark] - public void Empty5() - { - } - } -} \ No newline at end of file diff --git a/src/BenchmarkDotNet.Core/Engines/Consumer.cs b/src/BenchmarkDotNet.Core/Engines/Consumer.cs index 90499df08..71471ee28 100644 --- a/src/BenchmarkDotNet.Core/Engines/Consumer.cs +++ b/src/BenchmarkDotNet.Core/Engines/Consumer.cs @@ -1,13 +1,69 @@ using System.Runtime.CompilerServices; +using System.Threading; +// ReSharper disable NotAccessedField.Local namespace BenchmarkDotNet.Engines { public class Consumer { - [MethodImpl(MethodImplOptions.NoInlining)] - public void Consume(T x) - { - // TODO: Investigate how the current approach works with nanoseconds benchmarks - } + private volatile byte byteHolder; + private volatile sbyte sbyteHolder; + private volatile short shortHolder; + private volatile ushort ushortHolder; + private volatile int intHolder; + private volatile uint uintHolder; + private volatile bool boolHolder; + private volatile char charHolder; + private volatile float floatHolder; + private double doubleHolder; + private long longHolder; + private ulong ulongHolder; + private string stringHolder; + private object objectHolder; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(byte byteValue) => byteHolder = byteValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(sbyte sbyteValue) => sbyteHolder = sbyteValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(short shortValue) => shortHolder = shortValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(ushort ushortValue) => ushortHolder = ushortValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(int intValue) => intHolder = intValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(uint uintValue) => uintHolder = uintValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(bool boolValue) => boolHolder = boolValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(char charValue) => charHolder = charValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(float floatValue) => floatHolder = floatValue; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(double doubleValue) => Volatile.Write(ref doubleHolder, doubleValue); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(long longValue) => Volatile.Write(ref longHolder, longValue); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(ulong ulongValue) => Volatile.Write(ref ulongHolder, ulongValue); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(string stringValue) => Volatile.Write(ref stringHolder, stringValue); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(object objectValue) => Volatile.Write(ref objectHolder, objectValue); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Consume(T objectValue) => Volatile.Write(ref objectHolder, objectValue); } } \ No newline at end of file