test/micro/org/openjdk/bench/java/nio/CharBuffers.java
author zgu
Wed, 06 Nov 2019 09:50:53 -0500
changeset 58946 83810b7d12e7
parent 53901 0448cb38c418
permissions -rw-r--r--
8233401: Shenandoah: Refactor/cleanup Shenandoah load barrier code Reviewed-by: aph, shade, rkennke
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53901
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     1
/*
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     4
 *
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    10
 *
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    15
 * accompanied this code).
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    16
 *
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    20
 *
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    23
 * questions.
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    24
 */
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    25
package org.openjdk.bench.java.nio;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    26
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    27
import java.nio.CharBuffer;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    28
import java.util.Arrays;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    29
import java.util.concurrent.TimeUnit;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    30
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Benchmark;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.BenchmarkMode;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    33
import org.openjdk.jmh.annotations.Level;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    34
import org.openjdk.jmh.annotations.Mode;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    35
import org.openjdk.jmh.annotations.OutputTimeUnit;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    36
import org.openjdk.jmh.annotations.Param;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    37
import org.openjdk.jmh.annotations.Scope;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    38
import org.openjdk.jmh.annotations.Setup;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    39
import org.openjdk.jmh.annotations.State;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    40
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    41
@BenchmarkMode(Mode.AverageTime)
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    42
@OutputTimeUnit(TimeUnit.NANOSECONDS)
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    43
@State(Scope.Thread)
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    44
public class CharBuffers {
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    45
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    46
    @Param({"2", "256", "16384"})
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    47
    public int numChars;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    48
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    49
    public String str;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    50
    public CharBuffer buf;
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    51
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    52
    @Setup(Level.Iteration)
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    53
    public void createString() {
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    54
        char[] c = new char[numChars];
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    55
        Arrays.fill(c, 'X');
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    56
        str = String.valueOf(c);
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    57
        buf = CharBuffer.allocate(numChars);
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    58
    }
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    59
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    60
    @Benchmark
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    61
    public CharBuffer putString() {
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    62
        buf.clear();
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    63
        return buf.put(str);
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    64
    }
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    65
0448cb38c418 8011135: (bf) CharBuffer.put(String) is slow because of String.charAt() call for each char
bpb
parents:
diff changeset
    66
}