summaryrefslogtreecommitdiff
path: root/src/main/java/org/linaro/benchmarks/algorithm/NSieve.java
blob: 08a3feeb9055a02ff5aa3733391ffaa45c1643c4 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright (C) 2015 Linaro Limited. Ported to Java from:
 *  https://github.com/WebKit/webkit/blob/master/PerformanceTests/SunSpider/tests/sunspider-1.0.2/access-nsieve.js
 *
 */

// The Great Computer Language Shootout
//  http://shootout.alioth.debian.org/
//
//  modified by Isaac Gouy

// http://benchmarksgame.alioth.debian.org/license.html  (BSD 3-clause license)
// See NOTICE file for license.

package org.linaro.benchmarks.algorithm;

import java.lang.System;
import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Benchmark)


public class NSieve {
  /* Expected result for the standard benchmark setup */
  private static final int EXPECTED = 14302;
  /* Number of repeats (internal, not benchmark iterations) */
  private static final int NUM_SIEVES = 4;
  /* Array of flags - big enough for all standard test scenarios */
  private static boolean[] flags = new boolean[80001];
  private static int[] results = new int[NUM_SIEVES];

  private int nsieve(int m) {
    for (int i = 2; i <= m; i++) {
      flags[i] = true;
    }

    int count = 0;
    for (int i = 2; i <= m; i++) {
      if (flags[i]) {
        for (int k = i + i; k <= m; k += i) {
          flags[k] = false;
        }
        count++;
      }
    }
    return count;
  }

  /**
   * Find prime numbers in three sizes of pool, four times over
   * Repeat over number of iterations set by framework
   **/
  @Benchmark
  public void jmhTimeNSieveAccess() {
    for (int i = 0; i < NUM_SIEVES; i++) {
      int sum = 0;
      for (int o = 1; o <= 3; o++) {
        int m = (1 << o) * 10000;
        sum += nsieve(m);
      }
      results[i] = sum;
    }
  }
}