]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/scratch/org/simantics/databoard/tests/StringUTFTest.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / scratch / org / simantics / databoard / tests / StringUTFTest.java
1 package org.simantics.databoard.tests;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.DataInputStream;
6 import java.io.DataOutputStream;
7 import java.nio.charset.Charset;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.databoard.binding.StringBinding;
11
12 /**
13  *
14  * @author Toni Kalajainen <toni.kalajainen@iki.fi>
15  */
16 public class StringUTFTest {
17         
18         public static void main(String[] args) throws Exception {
19                 
20                 StringBinding b = Bindings.STRING;
21                 int c = 10000;
22                 String[] strs = new String[c];
23                 for (int i=0; i<c; i++) strs[i] = (String) b.createRandom(i);
24                 
25                                 
26                 {
27                         ByteArrayOutputStream baos = new ByteArrayOutputStream( 10*1024*1024 );
28                         DataOutputStream dos = new DataOutputStream( baos );            
29                         System.gc();
30                         long x = System.nanoTime();
31                         for (int i=0; i<c; i++) {
32                                 dos.writeUTF(strs[i]);
33                         }
34                         long y = System.nanoTime() - x;
35                         System.out.println("DataOutputStream.writeUTF: "+y);
36
37                         byte[] data = baos.toByteArray();
38                         DataInputStream dis = new DataInputStream( new ByteArrayInputStream( data ) );
39                         x = System.nanoTime();
40                         for (int i=0; i<c; i++) {
41                                 dis.readUTF();
42                         }
43                         y = System.nanoTime() - x;
44                         System.out.println("DataOutputStream.readUTF : "+y);
45                 }
46                 
47                 {
48                         Charset cs = Charset.forName("UTF8");
49                         ByteArrayOutputStream baos = new ByteArrayOutputStream( 10*1024*1024 );
50                         DataOutputStream dos = new DataOutputStream( baos );            
51                         System.gc();
52                         long x = System.nanoTime();
53                         for (int i=0; i<c; i++) {
54                                 byte[] data = strs[i].getBytes(cs);
55                                 dos.writeInt(data.length);
56                                 dos.write(data);
57                         }
58                         long y = System.nanoTime() - x;
59                         System.out.println("String#getBytes(cs)      : "+y);
60
61                         
62                         byte[] strData = new byte[1024];
63                         byte[] data = baos.toByteArray();
64                         DataInputStream dis = new DataInputStream( new ByteArrayInputStream( data ) );
65                         x = System.nanoTime();
66                         for (int i=0; i<c; i++) {
67                                 int len = dis.readInt();
68                                 dis.read(strData, 0, len);
69                                 new String(strData, cs);
70                         }
71                         y = System.nanoTime() - x;
72                         System.out.println("new String(byte[], cs)   : "+y);
73                 }
74                 
75                 
76         }
77
78 }