1/* 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.util.*; 25import java.nio.*; 26import java.nio.charset.*; 27 28public class StrCodingBenchmarkUTF8 { 29 30 public static void main(String[] args) throws Throwable { 31 32 final int itrs = Integer.getInteger("iterations", 100000); 33 final int size = 2048; 34 final int subsize = Integer.getInteger("subsize", 128); 35 final Random rnd = new Random(); 36 final int maxchar = 0x7f; 37 38 Charset charset = Charset.forName("UTF-8"); 39 final String csn = charset.name(); 40 final Charset cs = charset; 41 42 int[] starts = new int[] { 0, 0x80, 0x800, 0x10000}; 43 for (int nb = 1; nb <= 4; nb++) { 44 45 final CharsetEncoder enc = cs.newEncoder(); 46 47 char[] cc = new char[size]; 48 int i = 0; 49 while (i < size - 3) { 50 i += Character.toChars(starts[nb - 1] + rnd.nextInt(maxchar), cc, i); 51 } 52 53 final String string = new String(cc); 54 final byte[] bytes = string.getBytes(cs); 55 56 System.out.printf("%n--------%s[nb=%d]---------%n", csn, nb); 57 int sz = 12; 58 while (sz < size) { 59 System.out.printf(" [len=%d]%n", sz); 60 final byte[] bs = Arrays.copyOf(bytes, sz); 61 final String str = new String(bs, csn); 62 StrCodingBenchmark.Job[] jobs = { 63 new StrCodingBenchmark.Job("String decode: csn") { 64 public void work() throws Throwable { 65 for (int i = 0; i < itrs; i++) 66 new String(bs, csn); 67 }}, 68 69 new StrCodingBenchmark.Job("String decode: cs") { 70 public void work() throws Throwable { 71 for (int i = 0; i < itrs; i++) 72 new String(bs, cs); 73 }}, 74 75 new StrCodingBenchmark.Job("String encode: csn") { 76 public void work() throws Throwable { 77 for (int i = 0; i < itrs; i++) 78 str.getBytes(csn); 79 }}, 80 81 new StrCodingBenchmark.Job("String encode: cs") { 82 public void work() throws Throwable { 83 for (int i = 0; i < itrs; i++) 84 str.getBytes(cs); 85 }}, 86 }; 87 StrCodingBenchmark.time(StrCodingBenchmark.filter(null, jobs)); 88 sz <<= 1; 89 } 90 } 91 } 92} 93