-
Notifications
You must be signed in to change notification settings - Fork 272
/
CSieve.cs
52 lines (45 loc) · 1.39 KB
/
CSieve.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// Based on Eratosthenes Sieve Prime Number Program in C, Byte Magazine, January 1983.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace Benchstone.BenchI
{
[BenchmarkCategory(Categories.Runtime, Categories.Benchstones, Categories.JIT, Categories.BenchI)]
public class CSieve
{
public const int Iterations = 200;
const int Size = 8190;
[Benchmark(Description = nameof(CSieve))]
public bool Test() {
bool[] flags = new bool[Size + 1];
int count = 0;
for (int iter = 1; iter <= Iterations; iter++)
{
count = 0;
// Initially, assume all are prime
for (int i = 0; i <= Size; i++)
{
flags[i] = true;
}
// Refine
for (int i = 2; i <= Size; i++)
{
if (flags[i])
{
// Found a prime
for (int k = i + i; k <= Size; k += i)
{
// Cancel its multiples
flags[k] = false;
}
count++;
}
}
}
return (count == 1027);
}
}
}