Comments (3)
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.
The second case could be _ => -1,
, which might speed up the match.
from dart-petitparser.
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)
- Eliminate dynamic calls
- How to upgrade? HOT 7
- Getting line and colone in the code. HOT 2
- Improve error reporting of `ExpressionPaser` HOT 1
- Consider rolling meta back to ^1.8.0 HOT 1
- Greedy parse problem HOT 2
- Higher Unicode planes support? HOT 2
- Collecting Parsed result and passing it to next product HOT 1
- ReferenceParser prevents repeatString optimization HOT 1
- Projects that are using PetitParser HOT 1
- Parsing Latin1 characters HOT 4
- Alternative to deprecated `separatedBy(separator, includeSeparators: false)` HOT 2
- Choice parser behavior HOT 2
- use petitparser to recognize jsonDecode output HOT 1
- Question: parse any word or term HOT 2
- Alternative to ref() HOT 8
- NoSuchMethodError: Class 'SequenceParser<dynamic>' has no instance method '&'. HOT 2
- endless recursion when adding binary operation expressions to grammar HOT 3
- is there a parser that consumes a string that matches a regex? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart-petitparser.