gpt4 book ai didi

java - 执行缓慢并且耗尽堆空间(即使 vm args 设置为 2g)

转载 作者:行者123 更新时间:2023-12-02 08:37:11 25 4
gpt4 key购买 nike

我正在编写一个函数,该函数将树中的所有路径生成为 xpath 语句并将它们存储在下面的包中是一个天真的(抱歉,这很长),下面是我对其进行优化的尝试:

/**
* Create the structural fingerprint of a tree. Defined as the multiset of
* all paths and their multiplicities
*/
protected Multiset<String> createSF(AbstractTree<String> t,
List<AbstractTree<String>> allSiblings) {
/*
* difference between unordered and ordered trees is that the
* next-sibling axis must also be used
*
* this means that each node's children are liable to be generated more
* than once and so are memo-ised and reused
*/

Multiset<String> res = new Multiset<String>();

// so, we return a set containing:
// 1. the node name itself, prepended by root symbol

res.add("/" + t.getNodeName());
List<AbstractTree<String>> children = t.getChildren();

// all of the childrens' sets prepended by this one

if (children != null) {

for (AbstractTree<String> child : children) {

Multiset<String> sub = createSF(child, children);

for (String nextOne : sub) {
if (nextOne.indexOf("//") == 0) {
res.add(nextOne);
} else {
res.add("/" + nextOne);
res.add("/" + t.getNodeName() + nextOne);
}
}
}
}

// 2. all of the following siblings' sets, prepended by this one

if (allSiblings != null) {

// node is neither original root nor leaf
// first, find current node

int currentNodePos = 0;
int ptrPos = 0;

for (AbstractTree<String> node : allSiblings) {
if (node == t) {
currentNodePos = ptrPos;
}
ptrPos++;
}

// 3. then add all paths deriving from (all) following siblings

for (int i = currentNodePos + 1; i < allSiblings.size(); i++) {
AbstractTree<String> sibling = allSiblings.get(i);

Multiset<String> sub = createSF(sibling, allSiblings);

for (String nextOne : sub) {
if (nextOne.indexOf("//") == 0) {
res.add(nextOne);
} else {
res.add("/" + nextOne);
res.add("/" + t.getNodeName() + nextOne);
}
}
}
}
return res;
}

现在(当前)在子类中进行优化:

private Map<AbstractTree<String>, Multiset<String>> lookupTable = new HashMap<AbstractTree<String>, Multiset<String>>();

public Multiset<String> createSF(AbstractTree<String> t,
List<AbstractTree<String>> allSiblings) {

Multiset<String> lookup = lookupTable.get(t);
if (lookup != null) {
return lookup;
} else {

Multiset<String> res = super.createSF(t, allSiblings);

lookupTable.put(t, res);
return res;
}
}

我的问题是优化版本耗尽了堆空间(虚拟机参数设置为 -Xms2g -Xmx2g),并且在中等大小的输入上速度非常慢。有人能找到改进的方法吗?

最佳答案

通过分析器运行代码。这是获得有关代码的真实情况的唯一方法。其他一切都只是猜测。

关于java - 执行缓慢并且耗尽堆空间(即使 vm args 设置为 2g),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1271511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com