【问题标题】:Why is this Dart code so slow compared to java's implementation?为什么这个 Dart 代码比 java 的实现这么慢?
【发布时间】:2013-09-28 17:15:21
【问题描述】:

与java的实现相比,下面的dart代码非常慢。

//test.dart
import 'dart:io';
void main(){
for(int i = 0; i < 1 << 25;i++){
   stdout.write(i); // or directly print(i);
 }
  stdout.close();
}

java 版本:

//Test.java
import java.io.*;
public class Test{
public static void main(String[]args)throws Exception {
    try{
        PrintWriter out = new PrintWriter(System.out);
        for(int i = 0;i < 1 << 25; i++){
            out.print(i);
        }
        out.close();
    }catch(Exception e){}
  }
}

$ time java Test > /dev/null

real    0m6.421s
user    0m0.046s
sys     0m0.031s

$ time dart Test.dart > /dev/null

real    0m51.978s
user    0m0.015s
sys     0m0.078s

在 Dart 中默认情况下 stdout/print() 是无缓冲的吗?有没有类似 java 的 PrintWriter 的东西?谢谢。 (更新:预热 vm 后,stdout 比 java 慢 2 倍

real 0m15.497s   
user 0m0.046s   
sys 0m0.047s

================================================ =================================

2013 年 9 月 30 日更新

我已经为 dart 和 java 代码实现了自定义缓冲区,以便进一步比较,现在结果如下:

//test.dart
final int SIZE = 8192;
final int NUM = 1 << 25;
void main(){
  List<int> content = new List(SIZE);
  content.fillRange(0, SIZE, 0);
  for(int i = 0; i < NUM;i++){
    if(i % SIZE == 0 && i > 0)
        print(content);
    content[i % SIZE] = i;
  }
  if (NUM % SIZE ==0)
    print(content);
  else
    print(content.sublist(0, NUM % SIZE));
}

java 版本:

//Test.java
import java.util.Arrays;
public class Test{
public static final int SIZE = 8192;
public static final int NUM = 1 << 25;
public static void main(String[]args)throws Exception {
    try{
        int[] buf = new int[SIZE];
        for(int i = 0;i < NUM; i++){
            if(i % SIZE == 0 && i > 0)
                System.out.print(Arrays.toString(buf));
            buf[i % SIZE] = i;              
        }
        if(NUM % SIZE == 0)
            System.out.print(Arrays.toString(buf));
        else
        {
            int[] newbuf = new int[NUM % SIZE];
            newbuf = Arrays.copyOfRange(buf, 0, (NUM % SIZE));
            System.out.print(Arrays.toString(newbuf));
        }
        }catch(Exception e){}
    }
}

$ time java Test > /dev/null

real    0m7.397s
user    0m0.031s
sys     0m0.031s

$ time dart test.dart > /dev/null

real    0m22.406s
user    0m0.015s
sys     0m0.062s

如您所见,dart 仍然比 java 慢 3 倍。

【问题讨论】:

  • 对类似问题的answer 似乎获得了更快的速度:慢 2 倍与慢 3 倍。也许它可以帮助您进一步优化。
  • @PixelElephant:感谢您的回复。我已阅读答案并尝试将输出直接写入文件而不是在控制台上打印,但它仍然比 java 慢 3 倍。也许内部 dart 虚拟机或其他东西需要进一步优化。
  • 现在在 2018 年,使用 Dart VM 2.0.0 dev 62 的时间分别为 1。Dart:5.145 秒,2。Java:3.686 s,在我的笔记本电脑上。

标签: dart dart-io


【解决方案1】:

也许你的代码没有被 VM 优化。 只有“经常”使用的函数被编译并作为本机代码执行。 通常对于此类基准测试,您必须将测试代码放入一个函数并执行预热。例如:

//test.dart
import 'dart:io';
void f(nb_shift) {
for(int i = 0; i < 1 << nb_shift;i++){
   stdout.write(i); // or directly print(i);
 }
}

void main(){
  //warm up:
  f(3);
  // the test
  f(25);
  stdout.close();
}

尼古拉斯

【讨论】:

  • 感谢您的回复,真的很有帮助。预热后,现在 stdout 比 java 慢 2 倍。
猜你喜欢
  • 1970-01-01
  • 2017-01-10
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多