Java Speed Test

Please be patient for the timings to finish.
Source code is shown below.



These tests involve timing simple computational loops of many repetitions. The first four tests are of double precision math, with and without trigonometric functions, and with and temporary objects being allocated to save the results of the calculations. The last two tests are simple 32-bit and 64-bit integer arithmetic.



package org.shetline.SpeedTest001;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SpeedTestApp extends Applet implements Runnable, ActionListener
{
   private String[]     names = {
                           "double, trig, allocation",
                           "double, trig",
                           "double, allocation",
                           "double",
                           "int",
                           "long"
                        };
   private long[]       times = new long[names.length];
   private Button       restart = new Button("Restart");

   public SpeedTestApp()
   {
      clearTimes();
   }

   private void clearTimes()
   {
      for (int i = 0; i < times.length; ++i)
         times[i] = -1;
   }

   public void init()
   {
      setLayout(new BorderLayout());
      restart.setEnabled(false);
      restart.addActionListener(this);
      add(BorderLayout.SOUTH, restart);

      new Thread(this).start();
   }

   public synchronized void paint(Graphics g)
   {
      int   v;

      for (int i = 0; i < names.length; ++i) {
         v = i * 20 + 20;
         g.drawString(names[i] + ":", 10, v);

         if (times[i] < 0)
            g.drawString("---", 175, v);
         else
            g.drawString(times[i] + " msec", 175, v);
      }
   }

   public void run()
   {
      synchronized (this) {
         clearTimes();
      }

      repaint();

      for (int test = 0; test < times.length; ++test) {
         double   result = 0.0;
         long     start = System.currentTimeMillis();

         switch (test) {
            case 0:
               for (int i = 0; i < 4000000; ++i) {
                  double   z = i / 1000.0;
                  double   x = Math.sin(z);
                  double   y = Math.cos(z);
                  Foo      foo = new Foo(x, y);

                  result += x + y;
               }
               break;

            case 1:
               for (int i = 0; i < 4000000; ++i) {
                  double   z = i / 1000.0;
                  double   x = Math.sin(z);
                  double   y = Math.cos(z);

                  nada(x, y);
                  result += x + y;
               }
               break;

            case 2:
               for (int i = 1; i <= 4000000; ++i) {
                  double   z = i / 1000.0;
                  double   x = z * z + z;
                  double   y = (z - 1000) / z;
                  Foo      foo = new Foo(x, y);

                  result += y / x;
               }
               break;

            case 3:
               for (int i = 1; i <= 4000000; ++i) {
                  double   z = i / 1000.0;
                  double   x = z * z + z;
                  double   y = (z - 1000) / z;

                  nada(x, y);
                  result += y / x;
               }
               break;

            case 4:
               int   total = 0;

               for (int i = 0; i < 40000000; ++i) {
                  total += i;
                  total += (i % 49) / 7;
                  nada(total);
               }

               result = total;
               break;

            case 5:
               long  total2 = 0;

               for (long i = 0; i < 20000000L; ++i) {
                  total2 += i;
                  total2 += (i % 49) / 7;
                  nada(total2);
               }

               result = total2;
               break;
         }

         long  time = System.currentTimeMillis() - start;
         
         synchronized (this) {
            times[test] = time;
         }

         System.out.println("result[" + test + "]: " + result);
         repaint();
      }

      restart.setEnabled(true);
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getSource() == restart) {
         restart.setEnabled(false);
         new Thread(this).start();
       }
    }

   private void nada(double x, double y) {}
   private void nada(int x) {}
   private void nada(long x) {}

   private class Foo
   {
      public double  x;
      public double  y;
      
      public Foo(double x, double y)
      {
         this.x = x;
         this.y = y;
      }
   }
}

#!/usr/bin/perl -w use DBI; use DBD::mysql; use KSPrivate; require 'util.lib'; mimeText(); if (defined(getFormData('action'))) { $action = getFormData('action'); } else { $action = 'inc'; } $dbh = KSPrivate::connectToMyDB('shetline'); if (!$dbh) { print "???"; exit; } if ($action ne 'showall') { $id = getFormData('id'); unless ($id) { print "???"; exit; } $sth = $dbh->prepare("SELECT * FROM counters WHERE name = '$id'"); $sth->execute(); $counterExists = 0; while (@row = $sth->fetchrow_array) { $count = $row[1]; $counterExists = 1; last; } $sth->finish(); $showCount = 1; $saveCount = 1; if (getCookie('nocount') eq 'true' && $action eq 'inc') { $action = 'none'; } if ($action eq 'inc') { ++$count; } elsif ($action eq 'dec') { --$count; if ($count < 0) { $count = 0; } } elsif ($action eq 'set') { $count = getFormData('value'); } elsif ($action eq 'none') { $saveCount = 0; } else { print "???"; $saveCount = 0; $showCount = 0; } if ($showCount) { print $count; } if ($saveCount) { if ($counterExists) { $sth = $dbh->prepare("UPDATE counters SET counter = '$count' WHERE name = '$id'"); } else { $sth = $dbh->prepare("INSERT INTO counters (name, counter) VALUES('$id', '$count')"); } $sth->execute(); $dbh->commit(); } } else { $sth = $dbh->prepare("SELECT * FROM counters"); $sth->execute(); while (@row = $sth->fetchrow_array) { $name = $row[0]; $count = $row[1]; @rows = (@rows, "$name $count\n"); } @rows = sort(@rows); print "\n"; foreach $row (@rows) { print $row; } print "
\n"; } $dbh->disconnect();
#!/usr/bin/perl require 'util.lib'; mimeText(); $id = getFormData('id'); $action = getFormData('action'); if ($action ne 'logdump' && $action ne 'showlinks') { if (getCookie('nolog') eq 'true') { print "."; exit; } if (!defined($id)) { $id = 'unknown'; } $path = ">>logs/$id.log"; open(LOGFILE, $path) || errorExit(' '); flock(LOGFILE, 2); $timeStamp = gmtime; print LOGFILE "$timeStamp\t"; print LOGFILE "$ENV{'REMOTE_ADDR'}\t"; print LOGFILE "$ENV{'REMOTE_HOST'}\t"; print LOGFILE "$ENV{'HTTP_REFERER'}\t"; print LOGFILE "$ENV{'HTTP_USER_AGENT'}\t"; print LOGFILE "$ENV{'HTTP_ACCEPT_LANGUAGE'}\t"; print LOGFILE "$ENV{'HTTP_COOKIE'}\t"; print LOGFILE "\n"; flock(LOGFILE, 8); close(LOGFILE); } elsif ($action eq 'logdump') { print "\n"; print "\n"; print "Log\n"; print ''; print "\n"; print "\n"; if (defined($id)) { $file = "$id.log"; $path = "logs/$file"; if (open(LOGFILE, "logs/$file")) { showLog(); close(LOGFILE); } else { print "(error opening $file)\n"; } } else { opendir(LOGDIR, "logs") || errorExit(); @files = readdir(LOGDIR); closedir(LOGDIR); foreach $file (@files) { if ($file ne '.' && $file ne '..') { $path = "logs/$file"; if (open(LOGFILE, $path)) { showLog(); close(LOGFILE); } else { print "(error opening $file)\n"; } } } } print "\n"; print "\n"; } elsif ($action eq 'showlinks') { opendir(LOGDIR, "logs") || errorExit(); @files = readdir(LOGDIR); closedir(LOGDIR); @files = sort(@files); foreach $file (@files) { if ($file ne '.' && $file ne '..') { $file =~ s/\..*$//; print "View log for $file
\n"; } } } sub showLog { my @lines = ; @lines = reverse @lines; my $line; $file =~ s/\..*$//; print "$file
\n"; print "
\n"; foreach $line (@lines) { print ""; my @fields = split(/\t/, $line); my $i; my $count = (scalar @fields) - 1; if ($count == 6) { $fields[6] = $fields[5]; $fields[5] = $fields[4]; $fields[4] = $fields[3]; $fields[3] = ' '; ++$count; } my $count1 = $count - 1; my $field; my $doAlert; for ($i = 0; $i < $count; ++$i) { $field = $fields[$i]; if ($i == 1) { print ""; } elsif ($i != 6) { if (trim($field) eq '') { $field = ' '; } $longForm = ''; $doAlert = 0; if ($i == 3 && length($field) > 36) { $longForm = $field; $field = substr($field, 0, 36) . '...'; } elsif ($i == 3) { $longForm = $field; } elsif ($i == 4 && length($field) > 72) { $longForm = $field; $field = substr($field, 0, 72) . '...'; $doAlert = 1; } elsif ($i == 5 && length($field) > 6) { $longForm = $field; $field = substr($field, 0, 6) . '...'; $doAlert = 1; } if ($longForm ne '') { if ($doAlert) { $field = createAlertLink($field, $longForm); } else { $field = createLink($field, $longForm); } } print ""; } } print "\n"; if ($fields[6]) { $field = $fields[6]; my @items = split(/(&|%0D)/, $field); my $ccount = scalar @items; my $item; my $cookieStr = ''; for ($i = 0; $i < $ccount; ++$i) { $item = $items[$i]; if (length($item) > 150) { $item = substr($item, 0, 150) . '...'; } $cookieStr .= $item; if ($item eq '&' || $item eq '%0D') { $cookieStr .= '
'; } } print "\n"; } } print "
$field$field
$cookieStr
\n"; print "
\n"; } sub createAlertLink { my $shortForm = $_[0]; my $longForm = $_[1]; $longForm =~ s/"/\\"/g; return '' . $shortForm . ''; } sub createLink { my $shortForm = $_[0]; my $longForm = $_[1]; if ($longForm !~ /:\//) { if ($shortForm ne $longForm) { return createAlertLink($shortForm, $longForm); } else { return $longForm; } } return '' . $shortForm . ''; }