Java循环性能下降
情况是这样的 我有两个函数,函数A()和函数B()。函数A()有一个for循环,在每次迭代中调用函数B()。函数B()有一个for循环,循环次数约为1000万次Java循环性能下降,java,performance,loops,Java,Performance,Loops,情况是这样的 我有两个函数,函数A()和函数B()。函数A()有一个for循环,在每次迭代中调用函数B()。函数B()有一个for循环,循环次数约为1000万次 Function A() { for (i = 0; i < 10; i++) Function B(); } Function B() { for (i = 0; i < 10000000; i++) certain_operations() } 这里,打印1和打印2之间
Function A()
{
for (i = 0; i < 10; i++)
Function B();
}
Function B()
{
for (i = 0; i < 10000000; i++)
certain_operations()
}
这里,打印1和打印2之间的时间是函数A()的一次迭代的执行时间。
你知道为什么会出现这个问题吗?关于如何解决这个问题的建议将不胜感激。非常感谢
这是我的代码,以防出现异常
Function A()
{
for(Data d : packList)
List<Data> region = getRegionData(d, dataList, axisEN, eN, cluster++);
//dataList -> 10 million records
//d -> single instance of dataList
}
public List<Data> getRegionData(Data data, List<Data> listData, int axisEN, int eN, int cluster) //Function B()
{
List<Data> region = new ArrayList<Data>();
Data temp = new Data();
int distance;
Logger.sysLog(LogValues.info, this.getClass().getName(), "Print 1");
for(Data d : listData)
{
temp.setOffnet(java.lang.Math.abs(data.getOffnet() - d.getOffnet()));
temp.setOnnet(java.lang.Math.abs(data.getOnnet() - d.getOnnet()));
temp.setInet(java.lang.Math.abs(data.getInet() - d.getInet()));
distance = temp.getOffnet() + temp.getOnnet() + temp.getInet();
if(distance <= eN)
if(temp.getOffnet() <= axisEN && temp.getOnnet() <= axisEN && temp.getInet() <= axisEN)
{
d.setId(listData.indexOf(d));
d.setCluster(cluster);
d.setDistance(distance);
d.setCount(d.getCount() + 1);
region.add(d);
}
}
Logger.sysLog(LogValues.info, this.getClass().getName(), "Print 2");
Logger.sysLog(LogValues.info, this.getClass().getName(), "Cluster " + cluster + " : " + region.size());
return region;
}
}
打印三次迭代的详细信息
After Iter1
Heap
PSYoungGen total 18432K, used 952K [0x9ee80000, 0xa0300000, 0xb3540000)
eden space 15872K, 6% used [0x9ee80000,0x9ef6e248,0x9fe00000)
from space 2560K, 0% used [0xa0080000,0xa0080000,0xa0300000)
to space 2560K, 0% used [0x9fe00000,0x9fe00000,0xa0080000)
ParOldGen total 41728K, used 0K [0x76140000, 0x78a00000, 0x9ee80000)
object space 41728K, 0% used [0x76140000,0x76140000,0x78a00000)
PSPermGen total 16384K, used 1771K [0x72140000, 0x73140000, 0x76140000)
object space 16384K, 10% used [0x72140000,0x722faf00,0x73140000)
After Iter2
Heap
PSYoungGen total 18432K, used 952K [0x9ef00000, 0xa0380000, 0xb35c0000)
eden space 15872K, 6% used [0x9ef00000,0x9efee260,0x9fe80000)
from space 2560K, 0% used [0xa0100000,0xa0100000,0xa0380000)
to space 2560K, 0% used [0x9fe80000,0x9fe80000,0xa0100000)
ParOldGen total 41728K, used 0K [0x761c0000, 0x78a80000, 0x9ef00000)
object space 41728K, 0% used [0x761c0000,0x761c0000,0x78a80000)
PSPermGen total 16384K, used 1771K [0x721c0000, 0x731c0000, 0x761c0000)
object space 16384K, 10% used [0x721c0000,0x7237af00,0x731c0000)
After Iter3
Heap
PSYoungGen total 18432K, used 952K [0x9ee80000, 0xa0300000, 0xb3540000)
eden space 15872K, 6% used [0x9ee80000,0x9ef6e248,0x9fe00000)
from space 2560K, 0% used [0xa0080000,0xa0080000,0xa0300000)
to space 2560K, 0% used [0x9fe00000,0x9fe00000,0xa0080000)
ParOldGen total 41728K, used 0K [0x76140000, 0x78a00000, 0x9ee80000)
object space 41728K, 0% used [0x76140000,0x76140000,0x78a00000)
PSPermGen total 16384K, used 1771K [0x72140000, 0x73140000, 0x76140000)
object space 16384K, 10% used [0x72140000,0x722faf00,0x73140000)
线索是
listData.indexOf(d)
,这使得算法的复杂性是二次的。用一个简单的循环计数器替换
indexOf
,享受性能差异。只需我的2美分:
这些操作可能会导致性能问题
List<Data> region = new ArrayList<Data>();
Data temp = new Data();
...
region.add(..);
List region=new ArrayList();
数据温度=新数据();
...
地区.加入(……);
我会尝试以下步骤:
- 如果可能(!),尝试一次分配区域列表
- 尝试这样分配区域列表:
newarraylist(initSize)
不是必需的,请避免Data temp
尝试使用诸如float、int等primitivsnew
- 尝试在没有
记录器的情况下扰乱执行时间
-XX:+PrintGCDetails
JVM选项以查看堆中发生的情况。我在代码在三个不同时间运行时执行了PrintGCDetails。已包含编辑下的结果1@StackAddict我的意思是将JVM选项添加到实际的应用程序运行中。看起来您已经运行了3次单独的JVM。谢谢您的评论。。。实际上,我已经为关键操作编写了一个日志,即区域大小。也就是说,控件进入内部if()子句的次数。如果为零,则迭代的执行时间不到一秒钟。因此,if()子句中的语句会影响性能。此外,我需要处理区域数据。因此,除非有其他选择,否则我不能与region.add()妥协
After Iter1
Heap
PSYoungGen total 18432K, used 952K [0x9ee80000, 0xa0300000, 0xb3540000)
eden space 15872K, 6% used [0x9ee80000,0x9ef6e248,0x9fe00000)
from space 2560K, 0% used [0xa0080000,0xa0080000,0xa0300000)
to space 2560K, 0% used [0x9fe00000,0x9fe00000,0xa0080000)
ParOldGen total 41728K, used 0K [0x76140000, 0x78a00000, 0x9ee80000)
object space 41728K, 0% used [0x76140000,0x76140000,0x78a00000)
PSPermGen total 16384K, used 1771K [0x72140000, 0x73140000, 0x76140000)
object space 16384K, 10% used [0x72140000,0x722faf00,0x73140000)
After Iter2
Heap
PSYoungGen total 18432K, used 952K [0x9ef00000, 0xa0380000, 0xb35c0000)
eden space 15872K, 6% used [0x9ef00000,0x9efee260,0x9fe80000)
from space 2560K, 0% used [0xa0100000,0xa0100000,0xa0380000)
to space 2560K, 0% used [0x9fe80000,0x9fe80000,0xa0100000)
ParOldGen total 41728K, used 0K [0x761c0000, 0x78a80000, 0x9ef00000)
object space 41728K, 0% used [0x761c0000,0x761c0000,0x78a80000)
PSPermGen total 16384K, used 1771K [0x721c0000, 0x731c0000, 0x761c0000)
object space 16384K, 10% used [0x721c0000,0x7237af00,0x731c0000)
After Iter3
Heap
PSYoungGen total 18432K, used 952K [0x9ee80000, 0xa0300000, 0xb3540000)
eden space 15872K, 6% used [0x9ee80000,0x9ef6e248,0x9fe00000)
from space 2560K, 0% used [0xa0080000,0xa0080000,0xa0300000)
to space 2560K, 0% used [0x9fe00000,0x9fe00000,0xa0080000)
ParOldGen total 41728K, used 0K [0x76140000, 0x78a00000, 0x9ee80000)
object space 41728K, 0% used [0x76140000,0x76140000,0x78a00000)
PSPermGen total 16384K, used 1771K [0x72140000, 0x73140000, 0x76140000)
object space 16384K, 10% used [0x72140000,0x722faf00,0x73140000)
List<Data> region = new ArrayList<Data>();
Data temp = new Data();
...
region.add(..);