Skip to content

Leading zero count benchmark#60

Merged
LeeCampbell merged 2 commits intoHdrHistogram:masterfrom
LeeCampbell:LeadingZeroCountBenchmark
Jul 17, 2017
Merged

Leading zero count benchmark#60
LeeCampbell merged 2 commits intoHdrHistogram:masterfrom
LeeCampbell:LeadingZeroCountBenchmark

Conversation

@LeeCampbell
Copy link
Copy Markdown
Collaborator

Adds benchmarks for proposed LZC implementations from @bbarry in #42.
The proposed style can be made to outperform the current implementation for CLR using LegacyJit, however performs poorly on the RyuJit (which appears to be the faster of the Jitters in these benchmarks).

The concept proposed is solid, and in retrospect quite an obvious and elegant solution.
Basically this is a 32bit lzc implmentation, but for value greater than uint.Max we perform right shift of the higher bits, allowing us to reuse the 32 bit code.
I was actually surprised that it wasn't faster in the end especially the final revision below.

        private const long IntOverflow = int.MaxValue + 1L;
        public static int GetLeadingZeroCount(long value)
        {
            if (value < IntOverflow)
                return 63 - Log2((uint)value);
            return 32 - Log2((uint)(value >> 31));
        }

        private const int Bit24Range = 0x1000000 - 1;
        private const int Bit16Range = 0x10000 - 1;
        private const int Bit8Range = 0x100 - 1;
        private static int Log2(uint i)
        {
            if (i > Bit24Range) { return Lookup[i >> 24] + 24; }
            if (i > Bit16Range) { return Lookup[i >> 16] + 16; }
            if (i > Bit8Range) { return Lookup[i >> 8] + 8; }
            return Lookup[i];
        }

On the best case it has only (2 branches, 1 lookup, 3 math operations)
The worst case is either 4 branches, 1 lookup and 1 math operation, or, 3 branches 1 lookup and 3 math operations).
I am not sure of the cost of the cast long-> uint.

@AppVeyorBot
Copy link
Copy Markdown

1 similar comment
@AppVeyorBot
Copy link
Copy Markdown

@LeeCampbell
Copy link
Copy Markdown
Collaborator Author

Attached are the benchmark results from an Intel Core i7-4702HQ CPU 2.20GHz Haswell (Dell XPS-15 9530)

BenchmarkDotNet.Artifacts.Results.zip

@LeeCampbell LeeCampbell merged commit 41b16b0 into HdrHistogram:master Jul 17, 2017
@LeeCampbell LeeCampbell deleted the LeadingZeroCountBenchmark branch July 17, 2017 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants