On Thu, 20 Jun 2024 21:53:25 GMT, Justin Lu <j...@openjdk.org> wrote:

> > It requires append(int), but the Appendable has no such method.
> 
> If via `Appendable` we don't have access to `append(int)`, can we simply 
> `append(String.valueOf(int))`. And similarly for `append(char[], int, int)`, 
> can we `append(String.valueOf(char[], int, int))`.
> 
> According to the method descriptions in `AbstractStringBuilder`, this should 
> behaviorally be the same. That way we don't have to add any new classes, and 
> can continue with the generic implementation.

If do that ,there is some performance degradation.

Benchmark                                                      Mode  Cnt   
Score   Error  Units
AppendableTest.tesAppendableAppendCharAscii                    avgt   50  
33.628 ± 0.263  ns/op
AppendableTest.tesAppendableAppendCharMixNoAsciiChars          avgt   50  
33.522 ± 0.245  ns/op
AppendableTest.testAppendableAppendInt                         avgt   50  
32.602 ± 0.186  ns/op
AppendableTest.testStringBufferAppendCharArrayAscii            avgt   50  
31.028 ± 0.134  ns/op
AppendableTest.testStringBufferAppendCharArrayMixNoAsciiChars  avgt   50  
31.075 ± 0.158  ns/op
AppendableTest.testStringBufferAppendInt                       avgt   50  
25.706 ± 0.268  ns/op



@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class AppendableTest {

    private StringBuffer sb;
    private Appendable appendable;
    private char[] asciiChars;
    private char[] mixNoAsciiChars;

    @Setup(Level.Iteration)
    public void setup() {
        sb = new StringBuffer();
        appendable = sb;
        asciiChars = "Criticism appears to Anatole France the most recent and 
possibly the ultimate evolution".toCharArray();
        mixNoAsciiChars = "The 测试数据 above mentioned two volumes of poetry were 
followed by many works in prose, which we shall notice. France’s critical 
writings".toCharArray();
    }

    @Benchmark
    public void testStringBufferAppendInt() throws InterruptedException {
        sb.append(12345890);
    }

    @Benchmark
    public void testAppendableAppendInt() throws InterruptedException, 
IOException {
        appendable.append(String.valueOf(12345890));
    }

    @Benchmark
    public void testStringBufferAppendCharArrayAscii() throws 
InterruptedException {
        sb.append(asciiChars, 40, 18);
    }

    @Benchmark
    public void tesAppendableAppendCharAscii() throws InterruptedException, 
IOException {
        appendable.append(new String(asciiChars, 40, 18));
    }

    @Benchmark
    public void testStringBufferAppendCharArrayMixNoAsciiChars() throws 
InterruptedException {
        sb.append(mixNoAsciiChars, 40, 18);
    }

    @Benchmark
    public void tesAppendableAppendCharMixNoAsciiChars() throws 
InterruptedException, IOException {
        appendable.append(new String(mixNoAsciiChars, 40, 18));
    }
}

-------------

PR Comment: https://git.openjdk.org/jdk/pull/19513#issuecomment-2181851223

Reply via email to