다음과 같은 다트 코드는 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 배 느립니다.
[답] (http://stackoverflow.com/questions/16788467/console-print-speed) 비슷한 질문에 조금 더 빠른 속도를 얻을 것 같다 2 배 느린 대 3x 느린. 아마도 조금 더 최적화하는 데 도움이 될 것입니다. –
@ PixelElephant : 답장을 보내 주셔서 감사합니다. 답을 읽고 콘솔에 출력하는 대신 출력을 파일에 직접 쓰려고 시도했지만 여전히 Java보다 3 배 느립니다. 어쩌면 내부 다트 VM 또는 다른 뭔가가 더 최적화해야합니다. –