Skip to content

Instantly share code, notes, and snippets.

@melix
Created December 16, 2011 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/1486475 to your computer and use it in GitHub Desktop.
Save melix/1486475 to your computer and use it in GitHub Desktop.
/*
* Calculation of Pi using quadrature realized with a basic sequential algorithm and enforcing primitive
* types throughout.
*
* Copyright © 2008–2011 Russel Winder
*/
// This is Groovy and so extraordinarily slow compared to Java. Use primitive types though so as to avoid
// really bad performance due to use of Integer and BigDecimal. Hence the careful markup of the literals as
// well as the variables.
// Speed issue parameters: int is faster than long; classic for may be slower than "foreach" style for; **
// operator appears slow in comparison to using a variable and * —
// cf. pi_groovy_sequential_primitive_alt.groovy.
@groovy.transform.CompileStatic
def doIt() {
final int n = 100000000i // 10 times fewer due to speed issues.
final double delta = 1.0d / n
final startTime = System.nanoTime ( )
double sum = 0.0d
for ( int i=0; i<n; i++ ) { sum += 1.0d / ( 1.0d + ( ( i - 0.5d ) * delta ) ** 2i ) }
final double pi = 4.0d * sum * delta
final elapseTime = ( System.nanoTime ( ) - startTime ) / 1e9
println ( "==== Groovy Sequential Primitives pi = " + pi )
println ( "==== Groovy Sequential Primitives iteration count = " + n )
println ( "==== Groovy Sequential Primitives elapse = " + elapseTime )
}
doIt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment