打印语句对stdout性能和Dart-Editor与命令行性能 [英] Print statement versus stdout performance and Dart-Editor versus command-line performance

查看:293
本文介绍了打印语句对stdout性能和Dart-Editor与命令行性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能不是很重要,但是我注意到,在测试期间,打印语句的性能和stdout在Dart-Editor中比在命令行中快得多。从命令行,打印的性能比从命令行使用stdout要长大约36%。但是,从编辑器中运行程序,使用stdout比在编辑器中使用print语句要长大约900%,但是两者都比命令行快得多。即。

This is probably not of major importance, however I have noticed during testing that the performance of the print statement and also stdout is much faster in the Dart-Editor than from the command-line. From the command-line the performance of print takes around 36% longer than using stdout from the command-line. However, running the program from within the editor, using stdout takes around 900% longer than using the print statement in the editor, but both are considerably faster than from the command-line. ie. Print from a program running in the editor takes around 2.65% of the time it takes from the command-line.

根据我的测试的平均性能的一些相对时间:

Some relative timings based on average performance from my test :

Running program from command line (5000 iterations) :
print   1700 milliseconds.
stdout  1245 milliseconds.

Running program within Dart-Editor (5000 iterations) :
print     45 milliseconds
stdout   447 milliseconds.

有人可以向我解释这些差异的原因 - 特别是为什么在Dart-这么快?

Can someone explain to me the reason for these differences – in particular why performance in the Dart-Editor is so much faster? Also, is it acceptable practice to use stdout and what are the pros and cons versus using print?

推荐答案

为什么要使用stdout Dart编辑器更快?

因为命令行的输出处理真的很慢,这会阻塞输出流, / stdout。

Because the output handling by the command line is just really slow, and this blocks the output stream, and subsequently the call to print/stdout.

你可以自己测试一下 - 测试下面的java程序(当然有你自己的路径):

You can test this for yourself - test the following java program (with your own paths, of course):

public static void main(String[] args) {
    try {
        // the dart file does print and stdout in a loop
        Process p = Runtime.getRuntime().exec("C:\\eclipse\\dart-sdk\\bin\\dart.exe D:\\DEVELOP\\Dart\\Console_Playground\\bin\\console_playground.dart");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        StringBuffer buf = new StringBuffer();
        String line;
        while((line = in.readLine()) != null) {
            buf.append(line + "\r\n");
        }
        System.out.print(buf.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}



在我的机器上,这甚至比Dart编辑器(这可能像缓冲输入和定期渲染,但我不知道)。

On my machine, this is even slightly faster than the Dart Editor (which probably does something like buffering the input and rendering it periodically, but I don't really know).

你还会看到添加一个 Thread.sleep(1);

You will also see that adding a Thread.sleep(1); into the loop will severely impact the performance of the dart program, because the stream is blocked.

我认为这是非常主观的。我,一个,做任何让我更快地写代码。当我只想转储一个变量,我使用 print(myvar); 。但是使用stdout,你可以做这样的整洁: stdout.addStream(new File(rD:\test.csv)。openRead()); 。当然,如果性能是一个问题,它取决于您的应用程序将如何使用 - 例如,由另一个程序(打印更快)调用与命令行(其中stdout更快,由于某种原因)。

I think that's highly subjective. I, for one, do whatever lets me write code more quickly. When i just want to dump a variable, i use print(myvar);. But with stdout, you can do neat stuff like this: stdout.addStream(new File(r"D:\test.csv").openRead());. Of course, if performance is an issue, it depends on how your application will be used - for example, called by another program (where print is faster) vs. command line (where stdout is faster, for some reason).

为什么stdout在命令行中更快?

Why is stdout faster in command line?

我不知道,对不起。这是我测试print()的唯一环境,所以我猜测它与控制台处理传入数据的方式有关。

I have no idea, sorry. It's the only environment I tested where print() is slower, so I'd guess it has something to do with how the console handles incoming data.

这篇关于打印语句对stdout性能和Dart-Editor与命令行性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆