Skip to content

Commit 03c49a9

Browse files
committed
Improve benchmarks of sieves
1 parent 1751b19 commit 03c49a9

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

benchmark/Math/NumberTheory/SequenceBench.hs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE TupleSections #-}
2+
13
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
24

35
module Math.NumberTheory.SequenceBench
@@ -13,42 +15,33 @@ import Data.Maybe
1315
import Math.NumberTheory.Primes
1416

1517
filterIsPrime :: (Integer, Integer) -> Integer
16-
filterIsPrime (p, q) = sum $ takeWhile (<= q) $ dropWhile (< p) $ filter (isJust . isPrime) (map toPrim [toIdx p .. toIdx q])
18+
filterIsPrime (p, q) = sum $ takeWhile (<= p + q) $ dropWhile (< p) $ filter (isJust . isPrime) (map toPrim [toIdx p .. toIdx (p + q)])
1719

1820
eratosthenes :: (Integer, Integer) -> Integer
19-
eratosthenes (p, q) = sum (map unPrime [nextPrime p .. precPrime q])
21+
eratosthenes (p, q) = sum (map unPrime [nextPrime p .. precPrime (p + q)])
2022

2123
atkin :: (Integer, Integer) -> Integer
22-
atkin (p, q) = toInteger $ sum $ atkinPrimeList $ atkinSieve (fromInteger p) (fromInteger $ q - p)
24+
atkin (p, q) = toInteger $ sum $ atkinPrimeList $ atkinSieve (fromInteger p) (fromInteger q)
2325

2426
filterIsPrimeBench :: Benchmark
2527
filterIsPrimeBench = bgroup "filterIsPrime" $
26-
[ bench (show (10^x, 10^y)) $ nf filterIsPrime (10^x, 10^x + 10^y)
28+
[ bench (show (10^x, 10^y)) $ nf filterIsPrime (10^x, 10^x)
2729
| x <- [5..8]
2830
, y <- [3..x-1]
2931
]
3032

31-
eratosthenesBench :: Benchmark
32-
eratosthenesBench = bgroup "eratosthenes" $
33-
[ bench (show (10^x, 10^y)) $ nf eratosthenes (10^x, 10^x + 10^y)
34-
| x <- [10..17]
35-
, y <- [6..x-1]
36-
, x == 10 || y == 7
37-
]
38-
39-
atkinBench :: Benchmark
40-
atkinBench = bgroup "atkin" $
41-
[ bench (show (10^x, 10^y)) $ nf atkin (10^x, 10^x + 10^y)
42-
| x <- [10..17]
43-
, y <- [6..x-1]
44-
, x == 10 || y == 7
33+
sieveBench :: Benchmark
34+
sieveBench = bgroup "sieve" $ concat
35+
[ [ bench ("eratosthenes/" ++ show (10^x, 10^y)) $ nf eratosthenes (10^x, 10^y)
36+
, bench ("atkin/" ++ show (10^x, 10^y)) $ nf atkin (10^x, 10^y)
37+
]
38+
| (x, y) <- map (10,) [6..9] ++ map (,7) [10..16]
4539
]
4640

4741
benchSuite :: Benchmark
4842
benchSuite = bgroup "Sequence"
49-
[ filterIsPrimeBench
50-
, eratosthenesBench
51-
, atkinBench
43+
[ sieveBench
44+
, filterIsPrimeBench
5245
]
5346

5447
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)