简体   繁体   English

如何提高 c++ 目标上的 antlr4 运行时性能?

[英]How to improve antlr4 runtime performance on c++ target?

My tool intention is calculate the results parsed from antlr4.我的工具意图是计算从 antlr4 解析的结果。 The inputs had break into many lines, eg: 1+1, 3*4, ... Each line will init the parser and call the visitor.输入分为多行,例如:1+1、3*4、...每一行都会初始化解析器并调用访问者。 Therefore, if there is 1000 lines, it will call 1000 times of antlr4 parser.因此,如果有 1000 行,就会调用 antlr4 解析器 1000 次。 It run perfectly except the performances are slightly slow on the device.它运行完美,除了设备上的性能稍慢。 I hope to improve the performances even there is 1second faster.即使快了 1 秒,我也希望能提高性能。

I'm using antlr4.7 as runtime.我使用 antlr4.7 作为运行时。 The runtime target is on c++.运行时目标在 c++ 上。

for (itFormulas = fMap - > begin(); itFormulas != fMap - > end(); itFormulas++) 
{
  auto initStart = std::chrono::high_resolution_clock::now();

  ANTLRInputStream input(itFormulas - > second);
  FormulaLexer lexer( & input);
  CommonTokenStream tokens( & lexer);
  FormulaParser parser( & tokens);
  FormulaParser::MainContext * mainContext = parser.main();

  auto initEnd = std::chrono::high_resolution_clock::now();
  std::chrono::duration < double > totalInit = initEnd - initStart;

  //calculate time
  totalInitTime += totalInit.count();

  auto visitStart = std::chrono::high_resolution_clock::now();

  EvaluatorVisitor visitor(formulaMap, resultMap, inputMaps);

  try {
    antlrcpp::Any result = visitor.visit(mainContext - > children[0]);

    auto visitEnd = std::chrono::high_resolution_clock::now();
    std::chrono::duration < double > totalElapsed = visitEnd - visitStart;

    //calculate time
    totalVisitTime += totalElapsed.count();

    resultMap - > insert(pair < string, double > (itFormulas - > first, v));
  } catch (EvaluationException ex) {
    string from = "Mistake" + string(" itFormulas->") + itFormulas - > first;
  }
}

If the total routine will took 5seconds The parser time will be around 1 second, and the visitor time will be 4seconds.如果整个例程需要 5 秒,则解析器时间约为 1 秒,访问者时间为 4 秒。

I expect the time could be shorter, and wish to know which part can be optimised.我希望时间可以更短,并希望知道可以优化哪个部分。

由于https://github.com/antlr/antlr4/pull/3318 ,运行时更新 4.9.3 应该会提高性能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM