本文介绍了控制台打印速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一些示例程序,以找到更好的方式来使用Dart代码。

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

不是这个例子(下面)重要性,但是它取自rosettacode小点org与改变(希望)使它更新。

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.

这个发布的要点是关于基准,以及与其他语言相比,在打印到控制台的速度方面,Dart在某些基准测试中的结果可能是有害的。我不知道比较是什么(对于其他语言),但是在Dart,控制台输出(至少在Windows)看起来很慢,即使使用StringBuffer。

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.

另外,在我的测试中,如果n1允许增长到11,总递归计数=> 2.38亿,它需要(在我的笔记本电脑上)c。运行示例1 2.9秒。

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.

此外,可能感兴趣的是,如果将String赋值更改为int而不打印,则不会记录已用时间(示例2 )。

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

我的低规格笔记本电脑(从控制台 - Windows上运行)的典型时间。

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.

所以,任何人都可以告诉如何比较例如。 Java时间控制台输出?这至少可以说明Dart在这一领域是否特别缓慢,这可能与某些基准相关。顺便说一下,在Dart编辑器中运行的时间对打印造成的损失可以忽略不计。

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

测试的更改代码。

// 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 - Dart) - Java)。

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

每个的最佳时间如下(100,000次迭代):

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



Java 22秒。

Dart编辑器2.3秒。

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

虽然它不是破碎,它似乎表明,由于某种原因
(a)Dart是慢的控制台输出,和
(b)Dart-Editor是非常快的控制台输出。
(c)这需要采取

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.

也许当他们有时间:) Dart团队可以看看这个

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

示例1 - Dart

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

示例2 - Java

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

这篇关于控制台打印速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 06:12