Some Array vs GETFILED access times

1 message Options
Embed this post
Permalink
logicmoo

Some Array vs GETFILED access times

Reply Threaded More More options
Print post
Permalink
ABCL,
  For implementing MOP-like objects these times are something to consider.

  What I draw from it is when not messing with arrays (array bounds checking).
   Every case is better than.


JVM-L,
   Do these tests with times make sense?





package ArrayTests;

public class ArrayVsClass {

 public static void main(String[] args) {
  long lVectorStartTime = System.currentTimeMillis();
  int iterations = Integer.MAX_VALUE;
  while (iterations-- > 0) {
  // int iterations2 = 10;
   //while (iterations2-- > 0)
   {
       //testArray(); // ARRAY
    // vs
    //testGETFIELD(); // GETFIELD
    // vs
    testIBean(); // INVOKE INTERFACE
    // vs
    //testBean(); // INVOKE VIRTUAL
    // vs
    //testABean(); // INVOKE VIRTUAL POSSIBLY THROW
    // vs
    //testSlots(); // INVOKE FOR AVALUE
   }
  }
  long lVectorRunTime = System.currentTimeMillis() - lVectorStartTime;
  System.out.println("Bench time: " + lVectorRunTime);

 }

 // SLOTS time: 33157,33250,33156
 public static void testSlots() {
  ClassWithSlots oneSlot = new ClassWithSlots(6);

  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += oneSlot.getValue();
  }
 }
 
 // Array time: 18438,18437,18422
 public static void testArray() {
  final long[] accessArray = new long[] { 6 };
  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += accessArray[0];
  }
 }
 
 // GETFIELD time: 14688,14531,14453
 public static void testGETFIELD() {
  ClassWithOneSlot oneSlot = new ClassWithOneSlot(6);

  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += oneSlot.slot;
  }
 }

 // INVOKE VIRTUAL time: 14750,14594,14719
 public static void testBean() {
  ClassWithOneSlot oneSlot = new ClassWithOneSlot(6);

  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += oneSlot.getValue();
  }
 }
 
 // INVOKE INTERFACE time: 14469,14610,14859
 public static void testIBean() {
  IBeanWithOneSlot oneSlot = new ClassWithOneSlot(6);

  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += oneSlot.getValue();
  }
 }
 
 // INVOKE VIRTUAL POSSIBLY THROW time: 14641,14594,14547
 public static void testABean() {
  AClassWithOneSlot oneSlot = new ClassWithOneSlot(6);

  int iterations = Integer.MAX_VALUE;
  long result = 0;
  while (iterations-- > 0) {
   result += oneSlot.getValue();
  }
 }
 
 
 
 
 static interface IBeanWithOneSlot {
  public long getValue();
 }
 
 static class ClassWithOneSlot extends AClassWithOneSlot implements IBeanWithOneSlot {
  final public long slot;

  ClassWithOneSlot(long s) {
   slot = s;
  }

  @Override
  final public long getValue() {
   return slot;
  }
 }

 static class ClassWithSlots  {
  final public long[] slots = new long[1];

  ClassWithSlots(long s) {
   slots[0] = s;
  }

  final public long getValue() {
   return slots[0];
  }
 }

 static abstract class AClassWithOneSlot implements IBeanWithOneSlot {

  @Override
  public long getValue() {
   throw new NullPointerException();
  }
 
 }
}


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
armedbear-j-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/armedbear-j-devel