ope*_*sas 3 java performance bytecode fantom
从现在出现的许多jvm语言来看,有一种看起来特别吸引人
看一下
http://fantom.org/doc/docIntro/Tour.html
我只是想知道,当忽略动态类型功能时,生成的字节码是否与java等效...
ps:增加了关于性能的声明
我做了一些快速性能测试.
Int[100.000] array quicksort
Java ~ 11ms
Java using long and ArrayList<Long> ~ 66ms
Fantom ~ 97 ms
Int[1.000.000] array quicksort
Java ~ 91ms
Java using long and ArrayList<long> ~ 815ms
Fantom ~ 1100ms.
Run Code Online (Sandbox Code Playgroud)
所以我想说,目前Fantom的代码运行速度比Java代码慢10倍.但请注意,我使用的是Java的int和Fantom的Int,它们是不一样的.Java的int是32位而Fantom的是64位.
稍微分析一下后,有迹象表明Fantom代码几乎与Java一样.但是,如果性能绝对至关重要,请远离列表,使用特定于平台的列表版本或下拉到本机并使用Java编写.
编辑:我和布莱恩谈过话,他证实了我的怀疑.Fantom较慢的原因是因为它Int只是64位整数而且所有Int[]数组都相似ArrayList<Long>.新测试显示出差异.Fantom仍然较慢的原因可能是它的Duration,Int和List类比plain ArrayList或Integeror 有更多的方法/字段System.currentMillis.这是一种可能适合或不适合您的权衡.如果你真的需要高性能,只需制作一个本机方法并用Java/C#/ Javascript编程.这样你就可以获得Java等效的性能.
如果您想自己测试,请参考以下内容:
Fantom来源:
class TestQuickSort
{
public static Void swap(Int[] a, Int i, Int j) {
temp := a[i];
a[i] = a[j];
a[j] = temp;
}
public static Void quicksortA(Int[] a, Int L, Int R) {
m := a[(L + R) / 2];
i := L;
j := R;
while (i <= j) {
while (a[i] < m)
i++;
while (a[j] > m)
j--;
if (i <= j) {
swap(a, i, j);
i++;
j--;
}
}
if (L < j)
quicksortA(a, L, j);
if (R > i)
quicksortA(a, i, R);
}
public static Void quicksort(Int[] a) {
quicksortA(a, 0, a.size - 1);
}
static Void main(Str[] args) {
// Sample data
a := Int[,]
for(i := 0; i<1000000; i++)
{
a.add(i*3/2+1)
if(i%3==0) {a[i]=-a[i]}
}
t1 := Duration.now
quicksort(a);
t2 := Duration.now
echo((t2-t1).toMillis)
}
}
Run Code Online (Sandbox Code Playgroud)
并且用所有Ints替换为long和ArrayList的java.可以在http://stronglytypedblog.blogspot.com/2009/07/java-vs-scala-vs-groovy-performance.html找到原始Java实现.
import java.util.ArrayList;
public class QuicksortJava {
public static void swap(ArrayList<Long> a, long i, long j) {
long temp = a.get((int)i);
a.set((int)i, a.get((int)j));
a.set((int)j, temp);
}
public static void quicksort(ArrayList<Long> a, long L, long R) {
long m = a.get((int)(L + R) / 2);
long i = L;
long j = R;
while (i <= j) {
while (a.get((int)i) < m)
i++;
while (a.get((int)j) > m)
j--;
if (i <= j) {
swap(a, i, j);
i++;
j--;
}
}
if (L < j)
quicksort(a, L, j);
if (R > i)
quicksort(a, i, R);
}
public static void quicksort(ArrayList<Long> a) {
quicksort(a, 0, a.size() - 1);
}
public static void main(String[] args) {
// Sample data
long size = 100000;
ArrayList<Long> a = new ArrayList<Long>((int)size);
for (long i = 0; i < size; i++) {
a.add(i * 3 / 2 + 1);
if (i % 3 == 0)
a.set((int)i, -a.get((int)i));
}
long t1 = System.currentTimeMillis();
quicksort(a);
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
}
}
Run Code Online (Sandbox Code Playgroud)