Kotlin High Performance Programming

Kotlin中影响程序执行性能因素分析。

展开查看详情

1.Kotlin High Performance Programming 朱涛 Android Developer, Momo Inc.

2.History Java 1.0 in 1996 Kotlin 1.0 in 2016

3.Kotlin, a better Java? Safer More Concise More Productive Interoperable Tool-friendly

4.What about performance? CPU Memory FPS

5.Tools Show Kotlin Bytecode tools Benchmarks https://github.com/Kotlin/kotlin-benchmarks https://developer.android.com/kotlin https://openjdk.java.net/projects/code-tools/jmh/

6.

7.Show Kotlin Bytecode tools

8. Benchmarks apply plugin: 'kotlin-kapt' … dependencies { implementation 'org.openjdk.jmh:jmh-core:1.21' kapt 'org.openjdk.jmh:jmh-generator-annprocess:1.21' }

9.StringBuilder

10.Java world…

11.public static void stringAdd() { String string = ""; for(int i=0;i<10000;i++){ string += "hello"; } } public static void stringBuilder() { StringBuilder builder = new StringBuilder(); for(int i=0;i<10000;i++){ builder.append("hello"); } }

12.public static void stringAdd() { String string = ""; for(int i=0;i<10000;i++){ string += "hello"; ① } } public static void stringBuilder() { StringBuilder builder = new StringBuilder(); for(int i=0;i<10000;i++){ builder.append("hello"); ② } }

13.public static void stringAdd() { String string = ""; for(int i=0;i<10000;i++){ string += "hello"; ① } } public static void stringBuilder() { StringBuilder builder = new StringBuilder(); for(int i=0;i<10000;i++){ builder.append("hello"); ② } }

14.Kotlin world?

15.fun stringAdd() { var string = "" for (i in 0..9_999) { string += " kotlin" } }

16. SIPUSH 9999 … NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V SWAP INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/ lang/String;)Ljava/lang/StringBuilder; LDC " kotlin" … GOTO L2

17.SIPUSH 9999 … NEW java/lang/StringBuilder ① DUP INVOKESPECIAL java/lang/StringBuilder.<init> ② SWAP INVOKEVIRTUAL java/lang/StringBuilder.append ③ (Ljava/lang/String;)Ljava/lang/StringBuilder; LDC " kotlin" … GOTO L2

18.SIPUSH 9999 … NEW java/lang/StringBuilder ① DUP INVOKESPECIAL java/lang/StringBuilder.<init> ② SWAP INVOKEVIRTUAL java/lang/StringBuilder.append ③ (Ljava/lang/String;)Ljava/lang/StringBuilder; LDC " kotlin" … GOTO L2

19.fun stringBuilder() { val stringBuilder = StringBuilder() for (i in 0..9_999) { stringBuilder.append(" kotlin") } }

20.NEW java/lang/StringBuilder DUP INVOKESPECIAL java/lang/StringBuilder.<init> ()V … SIPUSH 9999 ISTORE 2 … LDC " kotlin" INVOKEVIRTUAL java/lang/StringBuilder.append (Ljava/lang/ String;)Ljava/lang/StringBuilder; … GOTO L2

21.NEW java/lang/StringBuilder ① DUP INVOKESPECIAL java/lang/StringBuilder.<init> ② … SIPUSH 9999 ISTORE 2 … LDC " kotlin" INVOKEVIRTUAL java/lang/StringBuilder.append ③ (Ljava/lang/String;)Ljava/lang/StringBuilder; … GOTO L2

22.NEW java/lang/StringBuilder ① DUP INVOKESPECIAL java/lang/StringBuilder.<init> ② … SIPUSH 9999 ISTORE 2 … LDC " kotlin" INVOKEVIRTUAL java/lang/StringBuilder.append ③ (Ljava/lang/String;)Ljava/lang/StringBuilder; … GOTO L2

23.1. Use StringBuilder in loop

24.1. Use StringBuilder in loop — Java coding experience is helpful.

25.Primitive

26. float Float int Integer double Double byte short Short Byte char Character Primitive

27. Float Int float Float int Integer double Double Double byte Byte short Short Byte Short char Char Character Primitive

28.Primitive or Wrapper

29. Primitive Wrapper int, float… Integer, Float… Object Oriented, Better Performance null instead of 0 Stack Heap