Advertisement
Guest User

FloatBuffer performance test

a guest
Sep 20th, 2013
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. package floatbuffertest;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.FloatBuffer;
  5. import java.util.Arrays;
  6. import java.util.LinkedList;
  7.  
  8. /**
  9.  *
  10.  * @author Two
  11.  */
  12. public class FloatBufferTest {
  13.  
  14.   private static class Testresult {
  15.  
  16.     public final String description;
  17.     public final long timeNS;
  18.  
  19.     public Testresult(final String description, final long timeNS) {
  20.       this.description = description + ":";
  21.       this.timeNS = timeNS;
  22.     }
  23.  
  24.     public void out(final long maxTimeNS) {
  25.       final double percent = ((double) timeNS) / ((double) maxTimeNS) * 100.0;
  26.       System.out.println(String.format("%-29s %15d ns %#6.2f%%", new Object[]{this.description, this.timeNS, percent}));
  27.     }
  28.   }
  29.   protected final static int SIZE = 16;
  30.   protected final static int ITERARIONS = 10000000;
  31.  
  32.   /**
  33.    * @param args the command line arguments
  34.    */
  35.   public static void main(String[] args) {
  36.     long ns = System.nanoTime(), largest; // warmup
  37.     int i, pos;
  38.     float value;
  39.     final LinkedList<Testresult> arrayResults = new LinkedList<>();
  40.     final LinkedList<Testresult> bufferResults = new LinkedList<>();
  41.     final LinkedList<Testresult> bufferDResults = new LinkedList<>();
  42.  
  43.     System.out.println("Using arrays of size " + SIZE + " with " + ITERARIONS + " iterations...");
  44.     final float[] fArray = new float[SIZE];
  45.     final FloatBuffer buffer = FloatBuffer.allocate(SIZE);
  46.     if (buffer.isDirect()) {
  47.       throw new RuntimeException("Non-direct buffer is direct!");
  48.     }
  49.     final FloatBuffer bufferD = ByteBuffer.allocateDirect(SIZE * 4).asFloatBuffer();
  50.     if (bufferD.isDirect() == false) {
  51.       throw new RuntimeException("Direct buffer is non-direct!");
  52.     }
  53.     final float[] helper = new float[SIZE];
  54.     final FloatBuffer bufferHelper = FloatBuffer.allocate(SIZE);
  55.     final FloatBuffer bufferDHelper = ByteBuffer.allocateDirect(SIZE * 4).asFloatBuffer();
  56.  
  57.     ns = System.nanoTime() - ns;
  58.     ns = System.nanoTime();
  59.     for (i = ITERARIONS; i > 0; --i) {
  60.       for (pos = SIZE - 1; pos >= 0; --pos) {
  61.         fArray[pos] = 1.0f;
  62.       }
  63.     }
  64.     arrayResults.add(new Testresult("Loop-write array", System.nanoTime() - ns));
  65.  
  66.     ns = System.nanoTime();
  67.     for (i = ITERARIONS; i > 0; --i) {
  68.       Arrays.fill(fArray, 1.0f);
  69.     }
  70.     arrayResults.add(new Testresult("Arrays.fill", System.nanoTime() - ns));
  71.  
  72.     ns = System.nanoTime();
  73.     for (i = ITERARIONS; i > 0; --i) {
  74.       for (pos = SIZE - 1; pos >= 0; --pos) {
  75.         helper[pos] = fArray[pos];
  76.       }
  77.     }
  78.     arrayResults.add(new Testresult("Loop-read array", System.nanoTime() - ns));
  79.  
  80.     ns = System.nanoTime();
  81.     for (i = ITERARIONS; i > 0; --i) {
  82.       System.arraycopy(fArray, 0, helper, 0, fArray.length);
  83.     }
  84.     arrayResults.add(new Testresult("System.arraycopy", System.nanoTime() - ns));
  85.  
  86.     bufferHelper.put(helper);
  87.     ns = System.nanoTime();
  88.     for (i = ITERARIONS; i > 0; --i) {
  89.       buffer.rewind();
  90.       for (pos = SIZE - 1; pos >= 0; --pos) {
  91.         buffer.put(1.0f);
  92.       }
  93.     }
  94.     bufferResults.add(new Testresult("Loop-put buffer", System.nanoTime() - ns));
  95.  
  96.     ns = System.nanoTime();
  97.     for (i = ITERARIONS; i > 0; --i) {
  98.       buffer.rewind();
  99.       for (pos = SIZE - 1; pos >= 0; --pos) {
  100.         buffer.put(pos, 1.0f);
  101.       }
  102.     }
  103.     bufferResults.add(new Testresult("Index-put buffer", System.nanoTime() - ns));
  104.  
  105.     ns = System.nanoTime();
  106.     for (i = ITERARIONS; i > 0; --i) {
  107.       buffer.rewind();
  108.       buffer.put(helper);
  109.     }
  110.     bufferResults.add(new Testresult("Bulk-put array->buffer", System.nanoTime() - ns));
  111.  
  112.     ns = System.nanoTime();
  113.     for (i = ITERARIONS; i > 0; --i) {
  114.       buffer.rewind();
  115.       buffer.put(bufferHelper);
  116.     }
  117.     bufferResults.add(new Testresult("Bulk-put buffer->buffer", System.nanoTime() - ns));
  118.  
  119.     ns = System.nanoTime();
  120.     for (i = ITERARIONS; i > 0; --i) {
  121.       buffer.rewind();
  122.       buffer.put(bufferDHelper);
  123.     }
  124.     bufferResults.add(new Testresult("Bulk-put bufferD->buffer", System.nanoTime() - ns));
  125.  
  126.     ns = System.nanoTime();
  127.     for (i = ITERARIONS; i > 0; --i) {
  128.       buffer.rewind();
  129.       for (pos = SIZE - 1; pos >= 0; --pos) {
  130.         value = buffer.get();
  131.       }
  132.     }
  133.     bufferResults.add(new Testresult("Loop-get buffer", System.nanoTime() - ns));
  134.  
  135.     ns = System.nanoTime();
  136.     for (i = ITERARIONS; i > 0; --i) {
  137.       buffer.rewind();
  138.       for (pos = SIZE - 1; pos >= 0; --pos) {
  139.         value = buffer.get(pos);
  140.       }
  141.     }
  142.     bufferResults.add(new Testresult("Index-get buffer", System.nanoTime() - ns));
  143.  
  144.     ns = System.nanoTime();
  145.     for (i = ITERARIONS; i > 0; --i) {
  146.       buffer.rewind();
  147.       buffer.get(helper);
  148.     }
  149.     bufferResults.add(new Testresult("Bulk-get buffer->array", System.nanoTime() - ns));
  150.  
  151.     ns = System.nanoTime();
  152.     for (i = ITERARIONS; i > 0; --i) {
  153.       bufferD.rewind();
  154.       for (pos = SIZE - 1; pos >= 0; --pos) {
  155.         bufferD.put(1.0f);
  156.       }
  157.     }
  158.     bufferDResults.add(new Testresult("Loop-put bufferD", System.nanoTime() - ns));
  159.  
  160.     ns = System.nanoTime();
  161.     for (i = ITERARIONS; i > 0; --i) {
  162.       bufferD.rewind();
  163.       for (pos = SIZE - 1; pos >= 0; --pos) {
  164.         bufferD.put(pos, 1.0f);
  165.       }
  166.     }
  167.     bufferDResults.add(new Testresult("Index-put bufferD", System.nanoTime() - ns));
  168.  
  169.     ns = System.nanoTime();
  170.     for (i = ITERARIONS; i > 0; --i) {
  171.       bufferD.rewind();
  172.       bufferD.put(helper);
  173.     }
  174.     bufferDResults.add(new Testresult("Bulk-put array->bufferD", System.nanoTime() - ns));
  175.  
  176.     ns = System.nanoTime();
  177.     for (i = ITERARIONS; i > 0; --i) {
  178.       bufferD.rewind();
  179.       bufferD.put(bufferHelper);
  180.     }
  181.     bufferDResults.add(new Testresult("Bulk-put buffer->bufferD", System.nanoTime() - ns));
  182.  
  183.     ns = System.nanoTime();
  184.     for (i = ITERARIONS; i > 0; --i) {
  185.       bufferD.rewind();
  186.       bufferD.put(bufferDHelper);
  187.     }
  188.     bufferDResults.add(new Testresult("Bulk-put bufferD->bufferD", System.nanoTime() - ns));
  189.  
  190.     ns = System.nanoTime();
  191.     for (i = ITERARIONS; i > 0; --i) {
  192.       bufferD.rewind();
  193.       for (pos = SIZE - 1; pos >= 0; --pos) {
  194.         value = bufferD.get();
  195.       }
  196.     }
  197.     bufferDResults.add(new Testresult("Loop-get bufferD", System.nanoTime() - ns));
  198.  
  199.     ns = System.nanoTime();
  200.     for (i = ITERARIONS; i > 0; --i) {
  201.       bufferD.rewind();
  202.       for (pos = SIZE - 1; pos >= 0; --pos) {
  203.         value = bufferD.get(pos);
  204.       }
  205.     }
  206.     bufferDResults.add(new Testresult("Index-get bufferD", System.nanoTime() - ns));
  207.  
  208.     ns = System.nanoTime();
  209.     for (i = ITERARIONS; i > 0; --i) {
  210.       bufferD.rewind();
  211.       bufferD.get(helper);
  212.     }
  213.     bufferDResults.add(new Testresult("Bulk-get bufferD->array", System.nanoTime() - ns));
  214.  
  215.  
  216.     largest = 0;
  217.     for (Testresult result : arrayResults) {
  218.       largest = Math.max(largest, result.timeNS);
  219.     }
  220.     for (Testresult result : bufferResults) {
  221.       largest = Math.max(largest, result.timeNS);
  222.     }
  223.     for (Testresult result : bufferDResults) {
  224.       largest = Math.max(largest, result.timeNS);
  225.     }
  226.  
  227.     System.out.println("-- Array tests -----------------------------------------");
  228.     for (Testresult result : arrayResults) {
  229.       result.out(largest);
  230.     }
  231.     System.out.println("-- Buffer tests ----------------------------------------");
  232.     for (Testresult result : bufferResults) {
  233.       result.out(largest);
  234.     }
  235.     System.out.println("-- Direct buffer tests ---------------------------------");
  236.     for (Testresult result : bufferDResults) {
  237.       result.out(largest);
  238.     }
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement