小编H-M*_*an2的帖子

Overriding与if语句的性能

我正在扩展和改进Java应用程序,该应用程序也使用小型DSL进行长时间运行的搜索(详细地说,它用于模型查找,是的,它通常是NP-Complete).

在此搜索过程中,我想在控制台上显示一个小进度条.由于DSL的通用结构,我无法计算整体搜索空间大小.因此我只能输出第一个"回溯"语句的进度.

现在问题是:我可以为每个回溯语句使用一个标志来指示此语句应该报告进度.在评估语句时,我可以使用if语句检查标志:

public class EvalStatement {
  boolean reportProgress;

  public EvalStatement(boolean report) {
     reportProgress = report;
  }

  public void evaluate() {
    int progress = 0;

    while(someCondition) {
      // do something

      // maybe call other statement (tree structure)

      if (reportProgress) {
         // This is only executed by the root node, i. e.,
         // the condition is only true for about 30 times whereas
         // it is false millions or billions of times
         ++progress;
         reportProgress(progress);
      }
    }
  } …
Run Code Online (Sandbox Code Playgroud)

java oop performance if-statement

5
推荐指数
2
解决办法
1779
查看次数

标签 统计

if-statement ×1

java ×1

oop ×1

performance ×1