Skip to content

Instantly share code, notes, and snippets.

@advancedxy
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advancedxy/bbf6eb58f0af94521cf2 to your computer and use it in GitHub Desktop.
Save advancedxy/bbf6eb58f0af94521cf2 to your computer and use it in GitHub Desktop.
comment
public int whileLoop(int);
descriptor: (I)I
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=2
0: iconst_0
1: istore_2
2: iload_2
3: iload_1
4: if_icmpge 16
7: iload_2
8: iconst_1
9: iadd
10: istore_2
11: iconst_2
12: pop
13: goto 2
16: iload_2
17: ireturn
LocalVariableTable:
Start Length Slot Name Signature
0 18 0 this L;
0 18 1 size I
2 15 2 c I
LineNumberTable:
line 16: 0
line 17: 2
line 18: 7
line 19: 11
line 21: 16
StackMapTable: number_of_entries = 2
frame_type = 252 /* append */
offset_delta = 2
locals = [ int ]
frame_type = 13 /* same */
public int foreachLoop(int);
descriptor: (I)I
flags: ACC_PUBLIC
Code:
stack=3, locals=3, args_size=2
0: iconst_0
1: istore_2
2: getstatic #31 // Field scala/runtime/RichInt$.MODULE$:Lscala/runtime/RichInt$;
5: getstatic #36 // Field scala/Predef$.MODULE$:Lscala/Predef$;
8: iconst_1
9: invokevirtual #39 // Method scala/Predef$.intWrapper:(I)I
12: iload_1
13: invokevirtual #43 // Method scala/runtime/RichInt$.to$extension0:(II)Lscala/collection/immutable/Range$Inclusive;
16: new #45 // class $anonfun$foreachLoop$1
19: dup
20: invokespecial #46 // Method $anonfun$foreachLoop$1."<init>":()V
23: invokevirtual #52 // Method scala/collection/immutable/Range$Inclusive.foreach:(Lscala/Function1;)V
26: iload_1
27: ireturn
LocalVariableTable:
Start Length Slot Name Signature
0 28 0 this L;
0 28 1 size I
2 25 2 c I
LineNumberTable:
line 25: 0
line 26: 5
line 27: 26
看一下 javap 的输出吧...对于 foreach 而言, 匿名函数是用匿名 class 来实现的. 然后匿名函数调用的时候也是调用匿名 class
的方法来实现. 用 jvisualvm 可以观察到跑 foreachLoop 产生了一堆的 java.lang.Integer 对象. 对于匿名函数 class 的具体实现不清楚
, 但只能说肯定是调用匿名方法的时候产生的. 于是, 这样的临时中间对象产生 size 个, 速度肯定不会快的. 另外, 如果 jvm 的 heap
size 默认只有256M, 对于 1e9 这样的 size, gc 肯定会在运算中 kick in. 这又是对性能的影响. 于是, 博主这边的性能差距只有100倍来
看, 肯定是机器的性能很不错, 且设置的 heap size 较大.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment