Benchmarks


Try to solve these problems yourself. Only come look here if your code doesn't work, and you want to see what you did wrong.
 

Simple Java Operations


// Java Time Tests..                                         // 2017 February 6

package seetime;
import java.util.Random;

public class JavaTestTime {

  final static int many = 10000,
      vary = 0; // vary=0 tests math, =1 unknown array size

  // static int[] src = new int[many];  int[] rez = new int[many];
  //   same time accessing these arrays

  static int variably(int whom) {
    if ((whom&3)==0) return whom+many;
    return variably(whom+1);} //~variably

  static void TimeTest() {
    int ix, nx, zx;
    long was = 0, now = 0, non = 0, orr = 0, pls = 0, mns = 0, mpy = 0, div = 0;
    int[] src = new int[many];  int[] rez = new int[many];
    Random randy = new Random();
    for (ix=0; ix<many; ix++) src[ix] = randy.nextInt(999)+1;
    was = System.currentTimeMillis();
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = src[nx]^rez[many-1-ix];
    now = System.currentTimeMillis();
    non = now-was;
    if (vary>0) {
      zx = variably(randy.nextInt(99)); // so compiler can't know array size
      src = new int[zx];
      rez = new int[zx];
      for (ix=0; ix<zx; ix++) {
        src[ix] = randy.nextInt(999)+1;
        rez[ix] = 0;}
      was = System.currentTimeMillis();
      for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
        rez[ix] = src[nx]^rez[many-1-ix];
      now = System.currentTimeMillis();
      pls = now-was;
      nx = many/1000;
      was = (long)(nx*nx); // *nx); // = how many million times we did this op
      System.out.println("Java times, " + was + "Mx, " + non + " / " + pls
          + " +" + (zx-many));
      if (was==0) return; // now = non/was; // z ms = zM ns / xM times = time in ns..
      now = non/was;
      ix = (int)(((non-now*was)*10)/was);
      non = now;
      now = pls/was;
      nx = (int)(((pls-now*was)*10)/was);
      pls = now;
      System.out.println("3x array access = " + non + "." + ix
          + " ns / " + pls + "." + nx + " ns");
      return;} //~if (vary>0)
    was = now;
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = (src[ix]|src[nx])^rez[many-1-ix];
    now = System.currentTimeMillis();
    orr = now-was;
    was = now;
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = (src[ix]+src[nx])^rez[many-1-ix];
    now = System.currentTimeMillis();
    pls = now-was;
    was = now;
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = (src[ix]-src[nx])^rez[many-1-ix];
    now = System.currentTimeMillis();
    mns = now-was;
    was = now;
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = (src[ix]*src[nx])^rez[many-1-ix];
    now = System.currentTimeMillis();
    mpy = now-was;
    // was = now;
    for (ix=0; ix<many; ix++) rez[ix] = 0; // for (zx=0; zx<many; zx++)
    for (ix=0; ix<many; ix++) for (nx=0; nx<many; nx++)
      rez[ix] = (src[ix]/src[nx])^rez[many-1-ix];
    now = System.currentTimeMillis();
    div = now-was;
    nx = many/1000;
    was = (long)(nx*nx); // *nx); // = how many million times we did this op
    System.out.println("Java times, " + was + "Mx, _ " + non + " | " + orr
        + " + " + pls + " - " + mns + " * " + mpy + " / " + div);
    if (was==0) return; // z ms = zM ns / xM times = time in ns..
    now = non/was;
    ix = (int)(((non-now*was)*10)/was);
    non = now;
    now = pls/was;
    nx = (int)(((pls-now*was)*10)/was);
    pls = now;
    System.out.println("3x array access = " + non + "." + ix
        + " ns;  simple op = " + pls + "." + nx + " ns");
    now = mpy/was;
    ix = (int)(((mpy-now*was)*10)/was);
    mpy = now;
    now = div/was;
    nx = (int)(((div-now*was)*10)/was);
    div = now;
    System.out.println("   mpy = " + mpy + "." + ix
        + " ns;  div = " + div + "." + nx + " ns");} //~TimeTest
 
   // Test Driver
   public static void main(String args[]) {
     TimeTest();
     TimeTest();}} //~JavaTestTime (TT)