benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Primes.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 24 Jan 2016 12:08:45 +0100
changeset 1854 826eb936c9a8
permissions -rw-r--r--
The sieve benchmark makes the testing more real than the fake matrix multiplication
jaroslav@1854
     1
/**
jaroslav@1854
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1854
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1854
     4
 *
jaroslav@1854
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1854
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1854
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1854
     8
 *
jaroslav@1854
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1854
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1854
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1854
    12
 * GNU General Public License for more details.
jaroslav@1854
    13
 *
jaroslav@1854
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1854
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1854
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1854
    17
 */
jaroslav@1854
    18
package org.apidesign.benchmark.sieve;
jaroslav@1854
    19
jaroslav@1854
    20
abstract class Primes {
jaroslav@1854
    21
    private final Natural natural;
jaroslav@1854
    22
    private Filter filter;
jaroslav@1854
    23
jaroslav@1854
    24
    protected Primes() {
jaroslav@1854
    25
        this.natural = new Natural();
jaroslav@1854
    26
    }
jaroslav@1854
    27
jaroslav@1854
    28
    int next() {
jaroslav@1854
    29
        for (;;) {
jaroslav@1854
    30
            int n = natural.next();
jaroslav@1854
    31
            if (filter == null || filter.accept(n)) {
jaroslav@1854
    32
                filter = new Filter(n, filter);
jaroslav@1854
    33
                return n;
jaroslav@1854
    34
            }
jaroslav@1854
    35
        }
jaroslav@1854
    36
    }
jaroslav@1854
    37
jaroslav@1854
    38
    protected abstract void log(String msg);
jaroslav@1854
    39
jaroslav@1854
    40
    public final int compute(int count) {
jaroslav@1854
    41
        int cnt = 0;
jaroslav@1854
    42
        int res;
jaroslav@1854
    43
        for (;;) {
jaroslav@1854
    44
            res = next();
jaroslav@1854
    45
            cnt += 1;
jaroslav@1854
    46
            if (cnt % 1000 == 0) {
jaroslav@1854
    47
                log("Computed " + cnt + " primes. Last one is " + res);
jaroslav@1854
    48
            }
jaroslav@1854
    49
            if (cnt >= count) {
jaroslav@1854
    50
                break;
jaroslav@1854
    51
            }
jaroslav@1854
    52
        }
jaroslav@1854
    53
        return res;
jaroslav@1854
    54
    }
jaroslav@1854
    55
}