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

UnrollFactor setting not working #1636

Closed
colgreen opened this issue Jan 9, 2021 · 3 comments
Closed

UnrollFactor setting not working #1636

colgreen opened this issue Jan 9, 2021 · 3 comments

Comments

@colgreen
Copy link

colgreen commented Jan 9, 2021

Example using 0.12.1 (runtime is .NET 5)

[InvocationCount(3, 1)]
public class Test
{
    public static void Main() => BenchmarkRunner.Run<Test>();

    [IterationSetup]
    public void MySetup() => Console.WriteLine("MySetup");

    [Benchmark]
    public void MyBenchmark()
    {
        Console.WriteLine("MyBenchmark");
        Thread.Sleep(100);
    }
}

(noting that the second argument to InvocationCount is unrollFactor, and actually defaults to 1 - but shown here set explicitly to 1).

Output...

MySetup
MyBenchmark
MyBenchmark
MyBenchmark
WorkloadWarmup   1: 3 op, 338580900.00 ns, 112.8603 ms/op
MySetup
MyBenchmark
MyBenchmark
MyBenchmark
WorkloadWarmup   2: 3 op, 327990300.00 ns, 109.3301 ms/op
MySetup
MyBenchmark
MyBenchmark
MyBenchmark
WorkloadWarmup   3: 3 op, 327250400.00 ns, 109.0835 ms/op
...

Also see #730

This affects some of the benchmarks in the dotnetcore repo, hence those affected benchmarks will currently be reporting potentially very misleading results. E.g. The Array.Sort() benchmarks are mostly running against already sorted arrays. See:

https://github.com/dotnet/performance/blob/master/src/benchmarks/micro/libraries/System.Collections/Sort.cs

@adamsitnik
Copy link
Member

[InvocationCount(3, 1)]

WorkloadWarmup 1: 3 op,

You have set the invocation count to 3 and unroll factor to 1, hence the code gets executed 3 times per iteration. This works as expected. I am not sure if I understand where the problem is?

@colgreen
Copy link
Author

Hi. Perhaps I have misunderstood then. The scenario is performance testing of Array.Sort, where we pre-allocate a large array at global startup time, fill it with random (unsorted) values as part of [IterationSetup]. Thus, we want to re-run [IterationSetup] once before each [Benchmark], otherwise the benchmark is sorting already sorted data.

My reading was that the Unroll=n would cause the Benchmark to run n times for each call to IterationSetup], hence I set Unroll=1, as discussed in #730.

So maybe I just need to use [InvocationCount(1, 1)] ?

@adamsitnik
Copy link
Member

My reading was that the Unroll=n would cause the Benchmark to run n times for each call to IterationSetup

I see. What you are looking for is InvocationCount, which sets the number of invocations per iteration. Unroll factor controls the manual loop unrolling which you most probably should not care about when using IterationSetup. This is why it's set to 1 by default.

You might find the following doc useful: https://benchmarkdotnet.org/articles/guides/how-it-works.html

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

No branches or pull requests

2 participants