Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The performance of PetitParserDart? #30

Closed
freewind opened this issue Jul 15, 2013 · 1 comment
Closed

The performance of PetitParserDart? #30

freewind opened this issue Jul 15, 2013 · 1 comment

Comments

@freewind
Copy link

I'm thinking to use PetitParserDart instead of RegRex in my dart program, since I find it easier to write the code in PetitParserDart for complex rules than regex. But I don't know if the performance is good enough.

So I just write a small test:

import "package:petitparser/petitparser.dart";

const s = "dfsdf 2323 efwe 3345 fewf  9897 dfsdf 2323 efwe 3345 fewf  9897 dfsdf 2323 efwe 3345 fewf  9897 dfsdf 2323 efwe 3345 fewf  9897";

main() {
    test(petitTest);
    test(regexTest);
}

test(x()) {
    var start = new DateTime.now();
    for (int i = 0;i < 100000;i++) {
        x();
    }
    var end = new DateTime.now();
    print("cost: ${end.millisecondsSinceEpoch - start.millisecondsSinceEpoch} ms");
}

var regexParser = new RegExp(r"\d+");

regexTest() {
    for (var m in regexParser.allMatches(s)) {
        m.group(0);
    }
}

var petitParser = (digit().plus().flatten() | any()).plus();

petitTest() {
    petitParser.parse(s).value;
}

The result is:

cost: 924 ms
cost: 959 ms

Seems they have very similar performance. I'm not sure if my test is correct, and I want to get more information about PetitParserDart from you.

Thank you ~

@renggli
Copy link
Member

renggli commented Jul 16, 2013

It depends.

PetitParser can be fast, depending on what you parse, how you wrote your parser and how you parse. PetitParser is more powerful, as you can have recursion and production actions. Additionally, PetitParser is not limited to strings, you can parse and match any list of objects. PetitParser gets slightly faster over time as the Dart VM starts to JIT its code.

Keep in mind that the semantics of regular expressions and parsing expression grammars are very different. For example, the star() and plus() operators are greedy, consuming as much input as possible. The operators starGreedy() and starLacy() that I recently added can help you there if you need.

Regarding the test you could write it using Parser.matchesSkipping(String) to make it simpler and look more like a regular expression:

final petitParser = digit().plus().flatten();

petitTest() {
  for (var m in petitParser.matchesSkipping(s)) {
    m;
  }
}

@renggli renggli closed this as completed Jul 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants