1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 // Based on Eratosthenes Sieve Prime Number Program in C, Byte Magazine, January 1983.
7 using Microsoft.Xunit.Performance;
9 using System.Runtime.CompilerServices;
12 [assembly: OptimizeForBenchmarks]
14 namespace Benchstone.BenchI
16 public static class CSieve
20 public const int Iterations = 1;
22 public const int Iterations = 200;
25 const int Size = 8190;
27 [MethodImpl(MethodImplOptions.NoInlining)]
29 bool[] flags = new bool[Size + 1];
31 for (int iter = 1; iter <= Iterations; iter++)
35 // Initially, assume all are prime
36 for (int i = 0; i <= Size; i++)
42 for (int i = 2; i <= Size; i++)
47 for (int k = i + i; k <= Size; k += i)
49 // Cancel its multiples
57 return (count == 1027);
61 public static void Test() {
62 foreach (var iteration in Benchmark.Iterations) {
63 using (iteration.StartMeasurement()) {
64 for (int i = 0; i < Iterations; i++) {
71 static bool TestBase() {
73 for (int i = 0; i < Iterations; i++) {
79 public static int Main() {
80 bool result = TestBase();
81 return (result ? 100 : -1);