1

I have an evaluating tree class. Each node has children in a strict order. A server has a list of such trees.

When a client successfully connects to the server, it sends a lot of different HashMaps to a selected tree for computation. Typical HashMaps have pairs: [Variable string name, Variable int value].

Each TreeNode has a complex condition, which can read variables and has operations such as AND, OR, XOR, comparing with other variables or numbers. Each TreeNode also has statements, which can read/write variables and put new variables to HashMaps, which can subsequently be read/written in another TreeNode.

Here is a simplified structure of the trees:

public static class TreeNode {
    public static abstract class Condition {
        public abstract boolean evaluate(HashMap<String, Integer> contex);
    }

    public static abstract class Statement {
        public abstract void execute(HashMap<String, Integer> contex);
    }

    private Condition condition;
    private List<Statement> statements;
    private List<TreeNode> children;

    public void run(final HashMap<String, Integer> contex) {
        if (condition != null && !condition.evaluate(contex)) {
            return;
        }

        for (final Statement statement : statements) {
            statement.execute(contex);
        }

        for (final TreeNode child : children) {
            child.run(contex);
        }
    }
}

My code currently performs about 200000 iterations/sec on an Intel i7 u3517 for a tree with 100 nodes and an input HashMap with 10 variables. How can I speed it up?

4

1 回答 1

0

如果您的语句和子语句可以并行运行并且您使用的是 Java 8,则可以使用parallelStream().

        statements.parallelStream().forEach((statement) -> {
            statement.execute(contex);
        });

        children.parallelStream().forEach((child) -> {
            child.run(contex);
        });
于 2015-04-07T15:43:37.193 回答