Skip to content

Commit d34e0a7

Browse files
committed
Adding more LZC benchmarks
1 parent b14a368 commit d34e0a7

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

HdrHistogram.Benchmarking/LeadingZeroCount/BBarry32BitIfShiftLookupWith64BitShiftBranch.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ private static int Log2(int i)
4646
public static class BBarry32BitIfShiftLookupWith64BitShiftBranch_2
4747
{
4848
private static readonly int[] Lookup;
49-
private const long IntOverflow = int.MaxValue + 1L;
5049
static BBarry32BitIfShiftLookupWith64BitShiftBranch_2()
5150
{
5251
Lookup = new int[256];
@@ -57,7 +56,7 @@ static BBarry32BitIfShiftLookupWith64BitShiftBranch_2()
5756
}
5857
public static int GetLeadingZeroCount(long value)
5958
{
60-
if (value < IntOverflow)
59+
if (value <= int.MaxValue)
6160
return 63 - Log2((uint)value);
6261
return 32 - Log2((uint)(value >> 31));
6362
}
@@ -70,4 +69,41 @@ private static int Log2(uint i)
7069
return Lookup[i];
7170
}
7271
}
72+
73+
/// <summary>
74+
/// Contributed from @BBarry at https://github.com/HdrHistogram/HdrHistogram.NET/issues/42
75+
/// This variation perfoms very well. Similar profile to the "Current Impl".
76+
/// Faster on LegacyJIT/CLR, but much slower on RyuJIT (CLR & Core)
77+
/// This just compares only lessThan operator instead of lessThanEqualTo. #completeness
78+
/// </summary>
79+
public static class BBarry32BitIfShiftLookupWith64BitShiftBranch_3
80+
{
81+
private static readonly int[] Lookup;
82+
private const long IntOverflow = int.MaxValue + 1L;
83+
static BBarry32BitIfShiftLookupWith64BitShiftBranch_3()
84+
{
85+
Lookup = new int[256];
86+
for (int i = 1; i < 256; ++i)
87+
{
88+
Lookup[i] = (int)(Math.Log(i) / Math.Log(2));
89+
}
90+
}
91+
public static int GetLeadingZeroCount(long value)
92+
{
93+
if (value < IntOverflow)
94+
return 63 - Log2((uint)value);
95+
return 32 - Log2((uint)(value >> 31));
96+
}
97+
98+
private const int Bit24Range = 0x1000000 - 1;
99+
private const int Bit16Range = 0x10000 - 1;
100+
private const int Bit8Range = 0x100 - 1;
101+
private static int Log2(uint i)
102+
{
103+
if (i > Bit24Range) { return Lookup[i >> 24] + 24; }
104+
if (i > Bit16Range) { return Lookup[i >> 16] + 16; }
105+
if (i > Bit8Range) { return Lookup[i >> 8] + 8; }
106+
return Lookup[i];
107+
}
108+
}
73109
}

HdrHistogram.Benchmarking/LeadingZeroCount/LeadingZeroCountBenchmarkBase.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public void OneOffValidationOfImplementations()
5656
{"DeBruijn128Bits", LeadingZeroCount.DeBruijn128Bits.GetLeadingZeroCount},
5757
{"BBarry32BitIfShiftLookupWith64BitShiftBranch", LeadingZeroCount.BBarry32BitIfShiftLookupWith64BitShiftBranch.GetLeadingZeroCount},
5858
{"BBarry32BitIfShiftLookupWith64BitShiftBranch_2", LeadingZeroCount.BBarry32BitIfShiftLookupWith64BitShiftBranch_2.GetLeadingZeroCount},
59+
{"BBarry32BitIfShiftLookupWith64BitShiftBranch_3", LeadingZeroCount.BBarry32BitIfShiftLookupWith64BitShiftBranch_3.GetLeadingZeroCount},
5960
{"BBarryIfShiftLookup", LeadingZeroCount.BBarryIfShiftLookup.GetLeadingZeroCount},
6061
};
6162
ValidateImplementations(expectedData, functions);
@@ -190,6 +191,16 @@ public int BBarry_imp1()
190191
return sum;
191192
}
192193
[Benchmark]
194+
public int BBarry_imp2()
195+
{
196+
var sum = 0;
197+
for (int i = 0; i < _testValues.Length; i++)
198+
{
199+
sum += LeadingZeroCount.BBarryIfShiftLookup.GetLeadingZeroCount(_testValues[i]);
200+
}
201+
return sum;
202+
}
203+
[Benchmark]
193204
public int BBarry_imp3()
194205
{
195206
var sum = 0;
@@ -200,16 +211,17 @@ public int BBarry_imp3()
200211
return sum;
201212
}
202213
[Benchmark]
203-
public int BBarry_imp2()
214+
public int BBarry_imp4()
204215
{
205216
var sum = 0;
206217
for (int i = 0; i < _testValues.Length; i++)
207218
{
208-
sum += LeadingZeroCount.BBarryIfShiftLookup.GetLeadingZeroCount(_testValues[i]);
219+
sum += LeadingZeroCount.BBarry32BitIfShiftLookupWith64BitShiftBranch_3.GetLeadingZeroCount(_testValues[i]);
209220
}
210221
return sum;
211222
}
212223

224+
213225
private class CalculationExpectation
214226
{
215227
public long Value { get; }

0 commit comments

Comments
 (0)