"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/benchmarks/microbenchmarks/lib/language/sync_star_semantics_bench.dart" (13 Nov 2020, 4275 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'package:flutter/widgets.dart';
    6 
    7 import '../common.dart';
    8 
    9 const int _kNumIterations = 1000;
   10 const int _kNumWarmUp = 100;
   11 
   12 void main() {
   13   final List<String> words = 'Lorem Ipsum is simply dummy text of the printing and'
   14     " typesetting industry. Lorem Ipsum has been the industry's"
   15     ' standard dummy text ever since the 1500s, when an unknown'
   16     ' printer took a galley of type and scrambled it to make a'
   17     ' type specimen book'.split(' ');
   18   final List<InlineSpanSemanticsInformation> data = <InlineSpanSemanticsInformation>[];
   19   for (int i = 0; i < words.length; i++) {
   20     if (i.isEven) {
   21       data.add(
   22         InlineSpanSemanticsInformation(words[i], isPlaceholder: false),
   23       );
   24     } else if (i % 2 == 0) {
   25       data.add(
   26         InlineSpanSemanticsInformation(words[i], isPlaceholder: true),
   27       );
   28     }
   29   }
   30   print(words);
   31 
   32   // Warm up lap
   33   for (int i = 0; i < _kNumWarmUp; i += 1) {
   34     combineSemanticsInfoSyncStar(data);
   35     combineSemanticsInfoList(data);
   36   }
   37 
   38   final Stopwatch watch = Stopwatch();
   39   watch.start();
   40   for (int i = 0; i < _kNumIterations; i += 1) {
   41     consumeSpan(combineSemanticsInfoSyncStar(data));
   42   }
   43   final int combineSemanticsInfoSyncStarTime = watch.elapsedMicroseconds;
   44   watch
   45     ..reset()
   46     ..start();
   47   for (int i = 0; i < _kNumIterations; i += 1) {
   48     consumeSpan(combineSemanticsInfoList(data));
   49   }
   50   final int combineSemanticsInfoListTime = watch.elapsedMicroseconds;
   51   watch
   52     ..reset()
   53     ..start();
   54 
   55   final BenchmarkResultPrinter printer = BenchmarkResultPrinter();
   56   const double scale = 1000.0 / _kNumIterations;
   57   printer.addResult(
   58     description: 'combineSemanticsInfoSyncStar',
   59     value: combineSemanticsInfoSyncStarTime * scale,
   60     unit: 'ns per iteration',
   61     name: 'combineSemanticsInfoSyncStar_iteration',
   62   );
   63   printer.addResult(
   64     description: 'combineSemanticsInfoList',
   65     value: combineSemanticsInfoListTime * scale,
   66     unit: 'ns per iteration',
   67     name: 'combineSemanticsInfoList_iteration',
   68   );
   69   printer.printToStdout();
   70 }
   71 
   72 String consumeSpan(Iterable<InlineSpanSemanticsInformation> items) {
   73   String result = '';
   74   for (final InlineSpanSemanticsInformation span in items) {
   75     result += span.text;
   76   }
   77   return result;
   78 }
   79 
   80 
   81 Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoSyncStar(List<InlineSpanSemanticsInformation> inputs) sync* {
   82   String workingText = '';
   83   String workingLabel;
   84   for (final InlineSpanSemanticsInformation info in inputs) {
   85     if (info.requiresOwnNode) {
   86       if (workingText != null) {
   87         yield InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel ?? workingText);
   88         workingText = '';
   89         workingLabel = null;
   90       }
   91       yield info;
   92     } else {
   93       workingText += info.text;
   94       workingLabel ??= '';
   95       if (info.semanticsLabel != null) {
   96         workingLabel += info.semanticsLabel;
   97       } else {
   98         workingLabel += info.text;
   99       }
  100     }
  101   }
  102   if (workingText != null) {
  103     yield InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel);
  104   } else {
  105     assert(workingLabel != null);
  106   }
  107 }
  108 
  109 Iterable<InlineSpanSemanticsInformation> combineSemanticsInfoList(List<InlineSpanSemanticsInformation> inputs) {
  110   String workingText = '';
  111   String workingLabel;
  112   final List<InlineSpanSemanticsInformation> result = <InlineSpanSemanticsInformation>[];
  113   for (final InlineSpanSemanticsInformation info in inputs) {
  114     if (info.requiresOwnNode) {
  115       if (workingText != null) {
  116         result.add(InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel ?? workingText));
  117         workingText = '';
  118         workingLabel = null;
  119       }
  120       result.add(info);
  121     } else {
  122       workingText += info.text;
  123       workingLabel ??= '';
  124       if (info.semanticsLabel != null) {
  125         workingLabel += info.semanticsLabel;
  126       } else {
  127         workingLabel += info.text;
  128       }
  129     }
  130   }
  131   if (workingText != null) {
  132     result.add(InlineSpanSemanticsInformation(workingText, semanticsLabel: workingLabel));
  133   } else {
  134     assert(workingLabel != null);
  135   }
  136   return result;
  137 }