官术网_书友最值得收藏!

  • Dart Cookbook
  • Ivo Balbaert
  • 453字
  • 2021-08-05 17:42:48

Profiling and benchmarking your app

One of the key success factors of Dart is its performance. The Dart VM has a built-in optimizer to increase the execution speed. This optimizer needs a certain amount of execution time before it has enough information to do its work. This is no problem for an app in production, but when testing, you must ensure this condition is met. In this recipe, we are going to focus on how to best benchmark your application.

Getting ready

The benchmark_harness package from the Dart team is built specifically for this purpose. Add it to the pubspec.yaml file and import it in your code. We will illustrate this with the template_benchmark app. Also, make sure that all the issues detected by the checked mode are solved (because these could have an effect on the execution speed) and that the app is run in the production mode (refer to the Setting up the checked and production modes recipe in Chapter 1, Working with Dart Tools).

How to do it...

Perform the following steps to get the benchmark harness working:

  1. Import the benchmark library:
    import 'package:benchmark_harness/benchmark_harness.dart';
    
  2. Define a class TemplateBenchmark with its own main() and run() methods. It is a subclass of class BenchmarkBase in the benchmark library. The benchmark is started from run():
    class TemplateBenchmark extends BenchmarkBase {
      const TemplateBenchmark() : super("Template");
    
     static void main() {
     new TemplateBenchmark().report();
     }
    
     void run() {
         fib(20);
      }
    // recursive algorithms:
      int fib(int i) {
        if (i < 2) return i;
        return fib(i-1) + fib(i-2);
      }
    
    // int fib(n) => n<2 ? n : fib(n-2) + fib(n-1);
    
    // iterative algorithm:
    //  int fib(int i){
    //    int a = 0; int b = 1;
    //    for (int n=a; n < b; n++) {
    //        a = a + b; b = a;
    //    }
    //    return a;
    //  }
    
    void setup() { }
    void teardown() { }
    }
  3. The following code starts the whole benchmark machinery:
    main() {
     TemplateBenchmark.main();
    }
  4. If we benchmark the Fibonacci algorithm for the two recursive implementations (with if and with the ternary operators) and the iterative algorithm, we get the following results:

    Template(RunTime): 482.392667631452 us.

    Template(RunTime): 498.00796812749 us.

    Template(RunTime): 0.2441818187589752 us.

As we expected, the iterative algorithm performs orders of magnitude better than working recursively.

How it works...

Create a new benchmark by extending the BenchmarkBase class (here, TemplateBenchmark). The benchmarked code is called from the run() method within this subclass. The setup() and teardown() function code are run before and after the benchmark to prepare for the benchmark or clean up after it. These are not taken into account for the benchmark itself. The top-level mai n() function runs the benchmark by calling main() from the subclass of BenchmarkBase.

See also

主站蜘蛛池模板: 布拖县| 油尖旺区| 柳林县| 连城县| 尤溪县| 烟台市| 青浦区| 武乡县| 安岳县| 延吉市| 五莲县| 新昌县| 三原县| 建阳市| 台东县| 潮安县| 普兰店市| 通江县| 三江| 合山市| 台北市| 荆州市| 青海省| 五寨县| 牙克石市| 韶山市| 西吉县| 朝阳市| 高唐县| 望谟县| 青海省| 嵩明县| 东方市| 陆河县| 唐河县| 揭东县| 休宁县| 武穴市| 龙泉市| 富源县| 宁阳县|