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

Performance of BigInteger.Pow isn't optimal when exponent is NPOT #31378

Open
mcraiha opened this issue Nov 2, 2019 · 2 comments
Open

Performance of BigInteger.Pow isn't optimal when exponent is NPOT #31378

mcraiha opened this issue Nov 2, 2019 · 2 comments
Labels
area-System.Numerics backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity tenet-performance Performance related issue
Milestone

Comments

@mcraiha
Copy link

mcraiha commented Nov 2, 2019

I am trying to implement VDF (verifiable delay function) with C# and I noticed BigInteger.Pow has quite big performance penalty if exponent is NPOT (Non-Power-Of-Two).

// * Summary *

BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
AMD Ryzen 7 1700, 1 CPU, 16 logical and 8 physical cores
.NET Core SDK=3.0.100
[Host] : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT
DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), X64 RyuJIT

Method Mean Error StdDev
PowNice 7.098 s 0.0324 s 0.0303 s
PowSlow 12.942 s 0.0972 s 0.0909 s

and code I used for testing

using System;
using System.Numerics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace benchmark
{
	public class BigIntegerPow
	{
		public BigIntegerPow()
		{
			
		}

		[Benchmark]
		public BigInteger PowNice() => BigInteger.Pow(13, 1 << 23);

		[Benchmark]
		public BigInteger PowSlow() => BigInteger.Pow(13, (1 << 23) - 1);
	}

	class Program
	{
		static void Main(string[] args)
		{
			var summary = BenchmarkRunner.Run<BigIntegerPow>();
		}
	}
}
@tannergooding
Copy link
Member

Powers of two are faster because it is mostly just repeated squaring of the existing value and a multiplication at the end, where-as non-powers of two requires repeated multiplication (which is more expensive).

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@tannergooding tannergooding added tenet-performance Performance related issue and removed untriaged New issue has not been triaged by the area owner labels Mar 5, 2020
@tannergooding tannergooding added this to the Future milestone Jun 23, 2020
Copy link
Contributor

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

@dotnet-policy-service dotnet-policy-service bot added backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics backlog-cleanup-candidate An inactive issue that has been marked for automated closure. no-recent-activity tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

3 participants