1 package org.simantics.db.tests.performance.java;
3 import gnu.trove.list.array.TByteArrayList;
5 import java.io.ByteArrayOutputStream;
6 import java.io.DataOutputStream;
7 import java.nio.ByteBuffer;
8 import java.nio.ByteOrder;
9 import java.util.Arrays;
11 import org.simantics.db.testing.base.TestCommonPerf;
13 public class BufferTest extends TestCommonPerf {
15 public static void test() throws Exception {
16 final boolean DEBUG = false;
17 final int BUFFER_SIZE = 100000000;
18 final int LOOP_SIZE = BUFFER_SIZE / 10;
20 ByteBuffer b = ByteBuffer.wrap(new byte[BUFFER_SIZE]);
21 long start = System.nanoTime();
22 b.order(ByteOrder.LITTLE_ENDIAN);
23 for(int i=0;i<LOOP_SIZE;i++) {
24 b.asShortBuffer().put((short)i);
25 b.asIntBuffer().put(i);
27 long duration = System.nanoTime() - start;
29 System.err.println("took " + 1e-9*duration);
32 DataOutputStream ds = new DataOutputStream(new ByteArrayOutputStream(BUFFER_SIZE));
33 long start2 = System.nanoTime();
34 for(int i=0;i<LOOP_SIZE;i++) {
38 long duration2 = System.nanoTime() - start2;
40 System.err.println("took " + 1e-9*duration2);
42 byte[] arr = new byte[BUFFER_SIZE];
44 long start3 = System.nanoTime();
46 for(int i=0;i<LOOP_SIZE;i++) {
48 arr[index++] = (byte)(s & 0xFF);
49 arr[index++] = (byte)(s >>> 8);
50 arr[index++] = (byte)(i & 0xFF);
51 arr[index++] = (byte)((i >>> 8) & 0xFF);
52 arr[index++] = (byte)((i >>> 16) & 0xFF);
53 arr[index++] = (byte)((i >>> 24) & 0xFF);
55 long duration3 = System.nanoTime() - start3;
57 System.err.println("took " + 1e-9*duration3);
58 System.err.println(Arrays.hashCode(arr));
62 TByteArrayList list = new TByteArrayList(BUFFER_SIZE);
63 long start4 = System.nanoTime();
64 for(int i=0;i<LOOP_SIZE;i++) {
66 list.add((byte)(s & 0xFF));
67 list.add((byte)(s >>> 8));
68 list.add((byte)(i & 0xFF));
69 list.add((byte)((i >>> 8) & 0xFF));
70 list.add((byte)((i >>> 16) & 0xFF));
71 list.add((byte)((i >>> 24) & 0xFF));
73 long duration4 = System.nanoTime() - start4;
75 System.err.println("took " + 1e-9*duration4);
76 System.err.println(list.hashCode());
80 byte[] arr2 = new byte[BUFFER_SIZE];
81 long start5 = System.nanoTime();
82 for(int i=0;i<LOOP_SIZE;i++) {
87 long duration5 = System.nanoTime() - start5;
89 System.err.println("took " + 1e-9*duration5);
90 System.err.println(Arrays.hashCode(arr2));
94 byte[] arr3 = new byte[BUFFER_SIZE];
95 long start6 = System.nanoTime();
96 System.arraycopy(arr, 0, arr3, 0, BUFFER_SIZE);
97 long duration6 = System.nanoTime() - start6;
99 System.err.println("took " + 1e-9*duration6);
103 static private int byteIndex = 0;
105 static private void writeShort(byte[] bytes, short s) {
106 bytes[byteIndex++] = (byte)(s & 0xFF);
107 bytes[byteIndex++] = (byte)(s >>> 8);
110 static private void writeInt(byte[] bytes, int i) {
111 bytes[byteIndex++] = (byte)(i & 0xFF);
112 bytes[byteIndex++] = (byte)((i >>> 8) & 0xFF);
113 bytes[byteIndex++] = (byte)((i >>> 16) & 0xFF);
114 bytes[byteIndex++] = (byte)((i >>> 24) & 0xFF);
117 public static void main(String[] args) {
120 } catch (Exception e) {