Skip to content

Commit

Permalink
Update Java pidigits program to fastest from Benchmarks Game site as …
Browse files Browse the repository at this point in the history
…of Feb 21 2013 for 4 core 64-bit hardware
  • Loading branch information
jafingerhut committed Feb 22, 2013
1 parent f320ccd commit 3340c57
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@




<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Clojure whole program benchmarks</title>
<meta content="Andy Fingerhut" name="author"></head><body>[<a href="Clojure-benchmarks.html">benchmarks main page</a>] [whole program benchmarks] [<a href="Clojure-expression-benchmarks.html">expression benchmarks</a>] [<a href="Clojure-version-history.html">Clojure version history</a>] [<a href="Hardware-and-software-used-for-Clojure-benchmarks.html">hardware and software used</a>]
<h1><a class="mozTocH1" name="mozTocId411050"></a>Clojure whole program benchmarks</h1>For each program, the tables below include:<br>
Expand Down Expand Up @@ -267,7 +268,7 @@ <h1><a class="mozTocH1" name="mozTocId411050"></a>Clojure whole program benchmar
</td>
<td>Arbitrary precision integer arithmetic<br>
</td>
<td>[<a href="pidigits.java-4.java">Java</a>]<br>
<td>[<a href="pidigits.java">Java</a>]<br>

[<a href="pidigits.clojure-2.clj">Clojure</a>]<br>

Expand Down
123 changes: 123 additions & 0 deletions clojure-benchmarks-results/pidigits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Isaac Gouy
*/

import java.math.BigInteger;

public class pidigits {
static final int L = 10;

public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
int j = 0;

PiDigitSpigot digits = new PiDigitSpigot();

while (n > 0){
if (n >= L){
for (int i=0; i<L; i++) System.out.print( digits.next() );
j += L;
} else {
for (int i=0; i<n; i++) System.out.print( digits.next() );
for (int i=n; i<L; i++) System.out.print(" ");
j += n;
}
System.out.print("\t:"); System.out.println(j);
n -= L;
}
}
}


class PiDigitSpigot {
Transformation z, x, inverse;

public PiDigitSpigot(){
z = new Transformation(1,0,0,1);
x = new Transformation(0,0,0,0);
inverse = new Transformation(0,0,0,0);
}

public int next(){
int y = digit();
if (isSafe(y)){
z = produce(y); return y;
} else {
z = consume( x.next() ); return next();
}
}

public int digit(){
return z.extract(3);
}

public boolean isSafe(int digit){
return digit == z.extract(4);
}

public Transformation produce(int i){
return ( inverse.qrst(10,-10*i,0,1) ).compose(z);
}

public Transformation consume(Transformation a){
return z.compose(a);
}
}


class Transformation {
BigInteger q, r, s, t;
int k;

public Transformation(int q, int r, int s, int t){
this.q = BigInteger.valueOf(q);
this.r = BigInteger.valueOf(r);
this.s = BigInteger.valueOf(s);
this.t = BigInteger.valueOf(t);
k = 0;
}

public Transformation(BigInteger q, BigInteger r, BigInteger s, BigInteger t){
this.q = q;
this.r = r;
this.s = s;
this.t = t;
k = 0;
}

public Transformation next(){
k++;
q = BigInteger.valueOf(k);
r = BigInteger.valueOf(4 * k + 2);
s = BigInteger.valueOf(0);
t = BigInteger.valueOf(2 * k + 1);
return this;
}

public int extract(int j){
BigInteger bigj = BigInteger.valueOf(j);
BigInteger numerator = (q.multiply(bigj)).add(r);
BigInteger denominator = (s.multiply(bigj)).add(t);
return ( numerator.divide(denominator) ).intValue();
}

public Transformation qrst(int q, int r, int s, int t){
this.q = BigInteger.valueOf(q);
this.r = BigInteger.valueOf(r);
this.s = BigInteger.valueOf(s);
this.t = BigInteger.valueOf(t);
k = 0;
return this;
}

public Transformation compose(Transformation a){
return new Transformation(
q.multiply(a.q)
,(q.multiply(a.r)).add( (r.multiply(a.t)) )
,(s.multiply(a.q)).add( (t.multiply(a.s)) )
,(s.multiply(a.r)).add( (t.multiply(a.t)) )
);
}
}
Loading

0 comments on commit 3340c57

Please sign in to comment.