Java Dialects and Relatives


Java text application

import java.lang.*;
import java.util.*;

public class ahl {
  public static void main(String[] argv) {
    int i,n,iter=0;
    double s=0,r=0,a,t2;
    long t1;
    Date D=new Date();
    System.out.println(
      "ahl.java -- The Java version of Ahl's simple benchmark\n");
    t1=D.getTime();
    do {
      D=new Date();
    } while (t1==D.getTime());
    t1=D.getTime();

    do {
      iter++;
      r=0; s=0;
      for (n=1;n<=100;n++) {
        a=n;
        for (i=1;i<=10;i++) {
          a=(double)Math.sqrt(a);
          r+=Math.random();
        }
        for (i=1;i<=10;i++) {
          a=a*a;
          r+=Math.random();
        }
        s+=a;
      }
      D=new Date();
    } while ((D.getTime()-t1)<20000);
    t2=((double)(D.getTime()-t1))/1000;
    System.out.println(iter+" iterations");
    System.out.println(t2/iter+" seconds per iteration");
    System.out.println("Accuracy "+Math.abs(1010.-s/5.));
    System.out.println("Random "+Math.abs(1000.-r));
  }
}


Java graphical application/applet

This uses some deprecated APIs. I promise to fix them later.

To run this applet in your browser, click here.


import java.applet.*;
import java.awt.*;
import java.util.*;

public class ahlapplet extends Applet
{
    Label[] l;
    Button b;

    public void init()
    {
        int i;
        resize(240, 240);
        setLayout(new GridLayout(7,1));
        add(new Label("Ahl's Simple Benchmark",Label.CENTER));
        add(new Label("Copyright (c) 1998 Eric Korpela",Label.CENTER));
        b=new Button("Press me to start");
        add(b);
        l=new Label[4];
        l[0]=new Label("Iterations:");
        l[1]=new Label("Seconds/iteration:");
        l[2]=new Label("Accuracy:");
        l[3]=new Label("Random:");
		
        for (i=0;i<4;i++) {
       	    add(l[i]);
        }
    }

    public boolean action(Event e, Object label) {
        ahl();
        return true;
    }

    private void ahl() 
    {
        int i,n,iter=0;
        double s=0,r=0,a,t2;
        long t1;
        b.setLabel("Running");
        l[0].setText("Iterations:           Running");
        l[1].setText("Seconds/iteration:    Running");
        l[2].setText("Accuracy:             Running");
        l[3].setText("Random:               Running");
        Date D=new Date();
        t1=D.getTime();
        do {
            D=new Date();
        } while (t1==D.getTime());
        t1=D.getTime();

        do {
            iter++;
            r=0; s=0;
            for (n=1;n<=100;n++) {
                a=n;
                for (i=1;i<=10;i++) {
                    a=(double)Math.sqrt(a);
                    r+=Math.random();
                }
                for (i=1;i<=10;i++) {
                    a=a*a;
                    r+=Math.random();
                }
                s+=a;
            }
            D=new Date();
        } while ((D.getTime()-t1)<20000);
        t2=((double)(D.getTime()-t1))/1000.0;
        l[0].setText("Iterations:           "+iter);
        l[1].setText("Seconds/iteration:    "+t2/iter);
        l[2].setText("Accuracy:             "+Math.abs(1010.0-s/5.0));
        l[3].setText("Random:               "+Math.abs(1000.0-r));
        b.setLabel("Press me to run again.");
    }

    public static void main(String args[])
    {
        Frame frame=new Frame("ahlapplet");
        frame.resize(frame.insets().left+frame.insets().right+360,
                     frame.insets().top+frame.insets().bottom+360);
        ahlapplet ahl = new ahlapplet();
        frame.add("Center",ahl);
        ahl.init();
        ahl.start();
        frame.show();
    }
}

JavaScript

To run this version in your browser click here

var t_seconds=1

function ahl() {
  // The JavaScript version of Ahl's simple benchmark
  // Copyright 1998 Eric Korpela
  var iter=0;
  var D=new Date();
  var t1=D.getTime();
  while (t1==D.getTime()) {
    D=new Date();
  }
  t1=D.getTime();
  while ((D.getTime()-t1)<(1000*t_seconds)) {
    var r=0.0;
    var s=0.0;
    iter++;
    for (var n=1;n<=100;n++) {
      a=1.0*n;
      for (var i=1;i<=10;i++) {
	a=Math.sqrt(a);
	r+=Math.random();
      }
      for (var i=1;i<=10;i++) {
	a=a*a;
	r+=Math.random();
      }
      s+=a;
    }
    D=new Date();
  }
  var t2=((1.0)*(D.getTime()-t1))/1000.0;
  document.write("<H1>Ahl's simple benchmark, the Javascript version.</H1>");
  document.write('<P>I ran the benchmark for '+t2+' seconds.');
  document.write('<HR><P>'+iter+' iterations');
  document.write('<P>'+t2/iter+' seconds per iteration');
  document.write('<P>accuracy='+Math.abs(1010.0-s/5.0));
  document.write('<P>random='+Math.abs(1000.0-r));
  document.write('<BR>');
  document.write('<hr>\n');
  document.write("<center><A HREF='homepage.html'>Back to Eric's Home Page</A></center>");
  document.write('Copyright © 1998 Eric Korpela');
  document.write('<a href="mailto:korpela@ssl.berkeley.edu"><address>');
  document.write('korpela@ssl.berkeley.edu</address></a>')
}


Copyright © 2001 Eric Korpela
korpela@ssl.berkeley.edu