Giter Club home page Giter Club logo

Comments (3)

renggli avatar renggli commented on August 10, 2024

True, your code example (switch2) is even a tiny bit faster than the original if-then-else (control):

control             10,180.044μs [10,144.016μs; 10,216.072μs]
switch1             13,883.637μs [13,871.288μs; 13,895.986μs]
                    -36.391% [-36.846%; -35.935%]
switch2             10,051.340μs [10,038.367μs; 10,064.313μs]
                    1.257% [0.896%; 1.617%]
switch3             13,957.153μs [13,942.120μs; 13,972.186μs]
                    -37.114% [-37.648%; -36.581%]

However, switch2 also yields the warnings The generic type 'Success<dynamic>' / 'Failure<dynamic>' should have explicit type arguments but doesn't. Fixing that (switch3) makes everything slow like the original switch1.

import 'dart:math';

import 'package:benchmark/benchmark.dart';
import 'package:more/collection.dart';
import 'package:petitparser/petitparser.dart';

final random = Random(42);
const context = Context('', 0);
final input = IntegerRange(1024 * 1024)
    .map<Result<String>>((_) => random.nextBool()
        ? context.success<String>('success', random.nextInt(0xffff))
        : context.failure<String>('failure', random.nextInt(0xffff)))
    .toList(growable: false);

Benchmark exercise(int Function(Result<String>) underTest) => () {
      for (var i = 0; i < input.length; i++) {
        underTest(input[i]);
      }
    };

void main() {
  experiments(
    control: exercise((result) => result.isSuccess ? result.position : -1),
    experiments: {
      'switch1': exercise((result) => switch (result) {
            Success(position: final position) => position,
            Failure() => -1,
          }),
      'switch2': exercise((result) => switch (result) {
            final Success s => s.position,
            Failure _ => -1,
          }),
      'switch3': exercise((result) => switch (result) {
            final Success<String> s => s.position,
            Failure<String> _ => -1,
          }),
    },
  );
}

from dart-petitparser.

RandalSchwartz avatar RandalSchwartz commented on August 10, 2024

The second case could be _ => -1,, which might speed up the match.

from dart-petitparser.

renggli avatar renggli commented on August 10, 2024

That makes it a bit faster, but not by much:

control             10,138.543μs [10,128.630μs; 10,148.455μs]
switch1             12,682.747μs [12,664.574μs; 12,700.920μs]
                    -25.095% [-25.285%; -24.905%]
switch2             10,060.908μs [10,058.125μs; 10,063.691μs]
                    0.765% [0.665%; 0.866%]
switch3             12,713.996μs [12,705.227μs; 12,722.765μs]
                    -25.403% [-25.565%; -25.242%]

from dart-petitparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.