-
-
Save timyates/8285479 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Project Euler # 10 | |
//http://projecteuler.net/problem=10 | |
@GrabResolver( name='s', root='https://oss.sonatype.org/repositories/snapshots' ) | |
@Grab(group='org.gperfutils', module='gbench', version='0.4.2-groovy-2.1') | |
@Grab( 'com.bloidonia:groovy-stream:0.7.0-SNAPSHOT' ) | |
import groovy.transform.* | |
import groovy.stream.Stream | |
import groovyx.gbench.Benchmark | |
@Field int MIN = 2 | |
@Field int MAX = 20000 | |
def usingStream(){ | |
def isPrime = {n-> | |
for( int i = MIN ; i < n ; i++ ) { | |
if( n % i == 0 ) return false | |
} | |
true | |
} | |
Stream.from( MIN..MAX ) filter { isPrime( it ) }.sum() | |
} | |
def usingStream2(){ | |
Closure<Boolean> isPrime = { Integer n -> | |
n == 2 || (MIN..Math.sqrt( n )).every { Integer it -> n % it != 0 } | |
} | |
Stream.from MIN..MAX filter { Integer it -> isPrime( it ) }.collect().sum() | |
} | |
def usingStream3(){ | |
ArrayList<Integer> primes = new ArrayList<Integer>( 10000 ) | |
def isPrime = { Integer n -> | |
primes.every { Integer it -> n % it != 0 } | |
} | |
Stream.from MIN..MAX filter { Integer it -> isPrime( it ) } tapWithIndex { Integer it, Integer idx -> primes.add( idx, it ) }.collect().sum() | |
} | |
def isPrime(num, primes) { | |
primes.find { it <= Math.sqrt(num) && it != 1 && it != num && num % it == 0 } == null | |
} | |
def oldAddPrimes(){ | |
def primes = [] | |
def min = MIN | |
def max = MAX | |
(min..max).findAll { it % 2 == 1 || it == 2 }.each { i -> | |
if (isPrime(i, primes)) | |
{ | |
primes.add(i); | |
} | |
} | |
primes.sum() //1709600813 | |
} | |
def addAllPrimes(){ | |
def primes = 0 | |
def min = 2 | |
def max = MAX | |
(min..max).each { x -> | |
if( x == 2 || (MIN..Math.sqrt(x)).every{ x % it != 0 } ) { | |
primes += x | |
} | |
} | |
primes //1709600811 | |
} | |
println usingStream() | |
println usingStream2() | |
println usingStream3() | |
println oldAddPrimes() | |
println addAllPrimes() | |
def r = benchmark( measureCpuTime:false ) { | |
'Stream'{usingStream()} | |
'Stream2'{usingStream2()} | |
'Stream3'{usingStream3()} | |
'oldAdd'{oldAddPrimes()} | |
'newAdd'{addAllPrimes()} | |
} | |
r.prettyPrint() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21171191 | |
21171191 | |
21171191 | |
21171191 | |
21171191 | |
Environment | |
=========== | |
* Groovy: 2.2.1 | |
* JVM: Java HotSpot(TM) 64-Bit Server VM (24.45-b08, Oracle Corporation) | |
* JRE: 1.7.0_45 | |
* Total Memory: 321 MB | |
* Maximum Memory: 3641 MB | |
* OS: Mac OS X (10.8.5, x86_64) | |
Options | |
======= | |
* Warm Up: Auto (- 60 sec) | |
* CPU Time Measurement: Off | |
Stream 2588738000 | |
Stream2 805203000 | |
Stream3 551480000 | |
oldAdd 484894000 | |
newAdd 768785500 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment