Print statement versus stdout performance and Dart

2019-07-24 20:33发布

问题:

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.

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?

回答1:

Why is the Dart Editor faster?

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.

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

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

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.

Should stdout be used?

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

Why is stdout faster in command line?

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.