2013-09-28 7 views
3

다음과 같은 다트 코드는 java 구현에 비해 매우 느립니다.이 다트 코드가 java 구현과 비교할 때 왜 그렇게 느린 것일까 요?

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

자바 버전 : 널 (null) 널 (null)

//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){} 
    } 
} 

$ 시간 자바 테스트>는/dev/

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

$ 시간 다트 Test.dart>는/dev/

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

Dart에서 stdout/print()가 기본적으로 버퍼링되지 않습니까? java의 PrintWriter와 같은 것이 있습니까? 감사. ==========================

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

을 (업데이트 VM을 예열 한 후, 표준 출력은 자바보다 2 배 느리다) ========================================================================================================== ===

업데이트 내가 할 두 다트와 자바 코드에 대한 사용자 정의 버퍼를 구현 한 2013

년 9 월 30 일 추가 비교, 이제 그 결과는 다음과 같다 :

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

자바 버전 :

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

보시다시피 널

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

$ 시간 다트 test.dart>/dev에 널 (null)

//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){} 
    } 
} 

$ 시간 자바 테스트>는/dev// , 다트는 여전히 자바보다 3 배 느립니다.

+0

[답] (http://stackoverflow.com/questions/16788467/console-print-speed) 비슷한 질문에 조금 더 빠른 속도를 얻을 것 같다 2 배 느린 대 3x 느린. 아마도 조금 더 최적화하는 데 도움이 될 것입니다. –

+0

@ PixelElephant : 답장을 보내 주셔서 감사합니다. 답을 읽고 콘솔에 출력하는 대신 출력을 파일에 직접 쓰려고 시도했지만 여전히 Java보다 3 배 느립니다. 어쩌면 내부 다트 VM 또는 다른 뭔가가 더 최적화해야합니다. –

답변

1

코드가 VM에 의해 최적화되지 않을 수도 있습니다. "자주 사용되는"함수 만 컴파일되어 원시 코드로 실행됩니다. 일반적으로 이러한 벤치 마크에서는 테스트 된 코드를 함수에 넣고 워밍업을 수행해야합니다. exemple의 경우 :

//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(); 
} 

니콜라스

+0

답장을 보내 주셔서 감사합니다. 예열 후, 표준 출력은 java보다 2 배 느립니다. –