Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerable Product extension method #55

Merged
merged 7 commits into from
Aug 25, 2022

Conversation

snowfrogdev
Copy link
Contributor

This extension method is similar to the Sum method, except that it multiplies the items in the IEnumerable instead of adding them. I've implemented it for every type of number (int, long, float, double, decimal) and have tested it extensively.

Benchmarks show that the chosen implementation offers significant performance over other common alternatives.

BenchmarkDotNet=v0.12.1, OS=Windows 10.0.22000
12th Gen Intel Core i9-12900K, 1 CPU, 24 logical and 16 physical cores
.NET Core SDK=6.0.400
  [Host]     : .NET Core 6.0.8 (CoreCLR 6.0.822.36306, CoreFX 6.0.822.36306), X64 RyuJIT
  DefaultJob : .NET Core 6.0.8 (CoreCLR 6.0.822.36306, CoreFX 6.0.822.36306), X64 RyuJIT

Method N Mean Error StdDev Ratio Gen 0 Gen 1 Gen 2 Allocated
ProductAggregate 1 12.285 ns 0.1644 ns 0.1538 ns 1.00 0.0020 - - 32 B
ProductForEach 1 10.475 ns 0.0928 ns 0.0868 ns 0.85 0.0020 - - 32 B
Product 1 3.064 ns 0.0372 ns 0.0348 ns 0.25 - - - -
ProductAggregate 10 41.003 ns 0.2373 ns 0.1981 ns 1.00 0.0020 - - 32 B
ProductForEach 10 33.809 ns 0.4208 ns 0.3514 ns 0.82 0.0020 - - 32 B
Product 10 5.888 ns 0.0842 ns 0.0787 ns 0.14 - - - -
ProductAggregate 100 364.044 ns 5.4466 ns 4.5481 ns 1.00 0.0019 - - 32 B
ProductForEach 100 299.335 ns 2.4880 ns 2.3272 ns 0.82 0.0019 - - 32 B
Product 100 47.687 ns 0.3365 ns 0.2983 ns 0.13 - - - -
ProductAggregate 1000 3,242.624 ns 22.2032 ns 17.3348 ns 1.00 - - - 32 B
ProductForEach 1000 2,874.950 ns 24.9146 ns 22.0861 ns 0.89 - - - 32 B
Product 1000 750.601 ns 7.5546 ns 7.0666 ns 0.23 - - - -

Discovered what seems to be a bug with BenchmarkDotNet, making a commit here to make it easy to come back and reproduce the problem.

It seems like ReturnValueValidator only runs `GlobalSetup` with the first `Params`.
@ardalis ardalis merged commit 53279c2 into ardalis:main Aug 25, 2022
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