fantom 生成的字节码与 java 等效字节码的性能一样吗?

发布于 2024-11-04 01:29:36 字数 241 浏览 7 评论 0原文

从当今出现的众多 jvm 语言中,有一种似乎特别有吸引力,

请查看

http:// /fantom.org/doc/docIntro/Tour.html

我只是想知道,当忽略动态类型功能时,生成的字节码的性能是否相当于 java...

ps:添加了有关性能的声明

from the many jvm languages appearing nowdays, there's one that seems to be particularly appealing

have a look at

http://fantom.org/doc/docIntro/Tour.html

I just wonder if, when ignoring the dynamic typing feature, the generated bytecode is performant equivalent to java...

ps: added aclaration about performance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ繥欢鉨o。 2024-11-11 01:29:36

我做了一些快速排序性能测试。

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.

所以我想说,目前 Fantom 的代码运行速度比 Java 的代码慢大约 10 倍。但请注意,我使用了 Java 的 int 和 Fantom 的 Int,它们并不相同。 Java 的 int 是 32 位,Fantom 的 int 是 64 位

经过一番分析后,有迹象表明 Fantom 代码的性能几乎与
爪哇。但是,如果性能绝对至关重要,请远离列表,使用平台特定版本的列表或下拉至本机并用 Java 编写。

编辑:我和布莱恩谈过,他证实了我的怀疑。 Fantom 速度较慢的原因是它的所有 Int 都是 64 位整数,并且所有 Int[] 数组都类似于 ArrayList。新的测试显示了差异。 Fantom 仍然较慢的原因可能是它的 Duration、Int 和 List 类比普通的 ArrayList 或 Integer 或 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)  

   }  
}

所有 Int 都被 long 和 ArrayList 替换的 java。原始 Java 实现可以在 http:// strongtypedblog.blogspot.com/2009/07/java-vs-scala-vs-groovy-performance.html

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);  

 }  

}  

I did some quicksort performance testing.

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.

So I'd say that at the moment Fantom's code runs about 10x slower than Java's code. However do note that I used Java's int and Fantom's Int which aren't the same. Java's int are 32-bit and Fantom's are 64-bit.

After profiling things a bit there are indications that Fantom code is almost performant as
Java. However if performance is absolutely critical, stay away from Lists, use platform specific versions of lists or drop down to native and write in Java.

EDIT: I had a talk with brian and he confirmed my suspicions. The reason Fantom is slower is because all it's Int are 64-bit integer and all Int[] arrays are similar to ArrayList<Long>. The new test show the difference. The reason Fantom is still slower is probably that its Duration, Int and List classes have a lot more methods/fields than plain ArrayList or Integer or System.currentMillis. It's a tradeoff that may or may not suit you. If you really need high performance just make a native method and program it in Java/C#/Javascript. That way you'll get Java equivalent performance.

Here are sources if you want to test it yourself:

Fantom source:

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)  

   }  
}

And java with all the Ints replaced by longs and ArrayList. Original Java implementation can be found at http://stronglytypedblog.blogspot.com/2009/07/java-vs-scala-vs-groovy-performance.html.

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);  

 }  

}  
泡沫很甜 2024-11-11 01:29:36

没有使用 fantom 的经验,但看起来 fantom 解释器可以使用 java、.net 或 JS 库,但这并不是 fantom 编译的字节码< /em> 可以通过 java、.net 或 javascript 直接读取。

话虽如此……我稍后会检查一下,看起来很有趣:)

I have no experience with fantom, but it looks like the fantom interpreter can use java, .net or JS libraries, but it's not that the fantom compiled bytecode can be read out of the box by java, .net or javascript.

Having said that... I'll check it later, it looks interesting :)

听你说爱我 2024-11-11 01:29:36

Fantom 编译为自己的字节码格式,称为“fcode” - 然后在运行时转换为 java 字节码或 IL - 有关更多详细信息,请参阅此页面:

http://fantom.org/doc/docLang/Deployment.html

JavaScript 的工作方式有点不同 - 实际的 JavaScript 源代码是在编译时从 Fantom 源代码生成的代码(以及 Fantom 运行时所需的所有元数据) - 生成可以直接在浏览器中运行的独立 js 文件。

Fantom compiles down to its own byte-code format called "fcode" - which is then translated to java byte code or IL at runtime - see this page for more details:

http://fantom.org/doc/docLang/Deployment.html

JavaScript works a bit different - actual JavaScript source code is produced at compile time from the Fantom source code (along with all the meta-data the Fantom runtime needs) - to produce a standalone js file you can run directly in your browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文