Console Print Speed

2019-07-28 01:49发布

问题:

I’ve been looking at a few example programs in order to find better ways to code with Dart.

Not that this example (below) is of any particular importance, however it is taken from rosettacode dot org with alterations by me to (hopefully) bring it up-to-date.

The point of this posting is with regard to Benchmarks and what may be detrimental to results in Dart in some Benchmarks in terms of the speed of printing to the console compared to other languages. I don’t know what the comparison is (to other languages), however in Dart, the Console output (at least in Windows) appears to be quite slow even using StringBuffer.

As an aside, in my test, if n1 is allowed to grow to 11, the total recursion count = >238 million, and it takes (on my laptop) c. 2.9 seconds to run Example 1.

In addition, of possible interest, if the String assignment is altered to int, without printing, no time is recorded as elapsed (Example 2).

Typical times on my low-spec laptop (run from the Console - Windows).

Elapsed Microseconds (Print) = 26002
Elapsed Microseconds (StringBuffer) = 9000
Elapsed Microseconds (no Printing)   = 3000

Obviously in this case, console print times are a significant factor relative to computation etc. times.

So, can anyone advise how this compares to eg. Java times for console output? That would at least be an indication as to whether Dart is particularly slow in this area, which may be relevant to some Benchmarks. Incidentally, times when running in the Dart Editor incur a negligible penalty for printing.

// Example 1. The base code for the test (Ackermann).

main() {
  for (int m1 = 0; m1 <= 3; ++m1) {
    for (int n1 = 0; n1 <= 4; ++n1) {
      print ("Acker(${m1}, ${n1}) = ${fAcker(m1, n1)}");
    }
  }
}

int fAcker(int m2, int n2) => m2==0 ? n2+1 : n2==0 ?
    fAcker(m2-1, 1) : fAcker(m2-1, fAcker(m2, n2-1));

The altered code for the test.

// Example 2 //
main() {
  fRunAcker(1);   // print
  fRunAcker(2);   // StringBuffer
  fRunAcker(3);   // no printing
}

void fRunAcker(int iType) {
  String sResult;
  StringBuffer sb1;
  Stopwatch oStopwatch = new Stopwatch();
  oStopwatch.start();
  List lType = ["Print", "StringBuffer", "no Printing"];
  if (iType == 2)   // Use StringBuffer
    sb1 = new StringBuffer();

  for (int m1 = 0; m1 <= 3; ++m1) {
    for (int n1 = 0; n1 <= 4; ++n1) {
      if (iType == 1)   // print
        print ("Acker(${m1}, ${n1}) = ${fAcker(m1, n1)}");
      if (iType == 2)   // StringBuffer
        sb1.write ("Acker(${m1}, ${n1}) = ${fAcker(m1, n1)}\n");
      if (iType == 3)   // no printing
        sResult = "Acker(${m1}, ${n1}) = ${fAcker(m1, n1)}\n";
    }
  }
  if (iType == 2)
    print (sb1.toString());
  oStopwatch.stop();
  print ("Elapsed Microseconds (${lType[iType-1]}) = "+ 
   "${oStopwatch.elapsedMicroseconds}");
}
int fAcker(int m2, int n2) => m2==0 ? n2+1 : n2==0 ?
    fAcker(m2-1, 1) : fAcker(m2-1, fAcker(m2, n2-1));

//Typical times on my low-spec laptop (run from the console).
   //   Elapsed Microseconds (Print) = 26002
   //   Elapsed Microseconds (StringBuffer) = 9000
   //   Elapsed Microseconds (no Printing)   = 3000

回答1:

I tested using Java, which was an interesting exercise.

The results from this small test indicate that Dart takes about 60% longer for the console output than Java, using the results from the fastest for each. I really need to do a larger test with more terminal output, which I will do.

In terms of "computational" speed with no output, using this test and m = 3, and n = 10, the comparison is consistently around 530 milliseconds for Java compared to 580 milliseconds for Dart. That is 59.5 million calls. Java bombs with n = 11 (238 million calls), which I presume is stack overflow. I'm not saying that is a definitive benchmark of much, but it is an indication of something. Dart appears to be very close in the computational time which is pleasing to see. I altered the Dart code from using the "question mark operator" to use "if" statements the same as Java, and that appears to be a bit faster c. 10% or more, and that appeared to be consistently the case.



回答2:

I ran a further test for console printing as shown below (example 1 – Dart), (Example 2 – Java).

The best times for each are as follows (100,000 iterations) :

Dart 47 seconds.
Java 22 seconds.
Dart Editor 2.3 seconds.

While it is not earth-shattering, it does appear to illustrate that for some reason
(a) Dart is slow with console output, and
(b) Dart-Editor is extremely fast with console output.
(c) This needs to be taken into account when evaluating any performance that involves console output, which is what initially drew my attention to it.

Perhaps when they have time :) the Dart team could look at this if it is considered worthwhile.

Example 1 - Dart


// Dart - Test 100,000 iterations of console output //

Stopwatch oTimer = new Stopwatch(); 

main() {
  // "warm-up"
  for (int i1=0; i1 < 20000; i1++) {
    print ("The quick brown fox chased ...");
  }


  oTimer.reset();
  oTimer.start();
  for (int i2=0; i2 < 100000; i2++) {
    print ("The quick brown fox chased ....");
  }
  oTimer.stop();
  print ("Elapsed time = ${oTimer.elapsedMicroseconds/1000} milliseconds");
}

Example 2 - Java


  public class console001
  {
  // Java - Test 100,000 iterations of console output 

  public static void main (String [] args)
  {
     //  warm-up
     for (int i1=0; i1<20000; i1++)
     {
        System.out.println("The quick brown fox jumped ....");
     }

     long tmStart = System.nanoTime();
     for (int i2=0; i2<100000; i2++)
     {
        System.out.println("The quick brown fox jumped ....");
     }

     long tmEnd = System.nanoTime() - tmStart;
     System.out.println("Time elapsed in microseconds = "+(tmEnd/1000));
  }
} 


标签: dart