【发布时间】:2017-09-11 16:16:21
【问题描述】:
在我对网络扩散的研究中,我有以下代码为顶点建模了一个轻量级框架。最初的原型来自python中的一个框架,我将其翻译成Java。我遇到的问题是,虽然这段代码运行得比它的 python 版本快得多,最多 10000 个顶点,但对于更多的顶点(100,000+),它会停止。事实上,python 版本在 1.2 分钟内执行,而 java 版本即使在执行 7 分钟后也没有返回。我不确定为什么相同的代码会在更多的顶点处发生故障,我需要帮助来修复代码。
import java.util.*;
public class Vertex
{
private int id;
private HashMap<Integer, Double> connectedTo;
private int status;
public Vertex(int key)
{
this.id = key;
this.connectedTo = new HashMap<Integer, Double>();
this.status = 0;
}
public void addNeighbour(int nbr, double weight)
{
this.connectedTo.put(nbr, weight);
}
public int getId()
{
return this.id;
}
public double getWeight(int nbr)
{
return this.connectedTo.get(nbr);
}
public int getStatus()
{
return this.status;
}
public Set<Integer> getConnections()
{
return this.connectedTo.keySet();
}
//testing the class
public static void main(String[] args)
{
int noOfVertices = 100000;
Vertex[] vertexList = new Vertex[noOfVertices];
for (int i = 0; i < noOfVertices; i++) {
vertexList[i] = new Vertex(i);
}
for (Vertex v : vertexList) {
int degree = (int)(500*Math.random()); //random choice of degree
int neighbourCount = 0; // count number of neighbours built up
while (neighbourCount <= degree) {
int nbr = (int) (noOfVertices * Math.random()); // randomly choose a neighbour
double weight = Math.random(); // randomly assign a weight for the relationship
v.addNeighbour(nbr, weight);
neighbourCount++;
}
}
}
}
作为参考,这段代码的python版本如下:
import random
class Vertex:
def __init__(self, key):
self.id = key
self.connectedTo = {}
def addNeighbor(self, nbr, weight=0):
self.connectedTo[nbr] = weight
def __str__(self):
return str(self.id) + ' connectedTo: ' \
+ str([x.id for x in self.connectedTo])
def getConnections(self):
return self.connectedTo.keys()
def getId(self):
return self.id
def getWeight(self, nbr):
return self.connectedTo[nbr]
if __name__ == '__main__':
numberOfVertices = 100000
vertexList = [Vertex(i) for i in range(numberOfVertices)] # list of vertices
for vertex in vertexList:
degree = 500*random.random()
# build up neighbors one by one
neighbourCount = 0
while neighbourCount <= degree:
neighbour = random.choice(range(numberOfVertices))
weight = random.random() # random choice of weight
vertex.addNeighbor(neighbour, weight)
neighbourCount = neighbourCount + 1
【问题讨论】:
-
我目前正在研究这个,很快就会发布一些优化的代码!
-
如果不进行分析就很难分辨,实际上几乎可以在任何地方。只是一个快速点:看一下
java.util.Random类,它有一个nextInt(bound)方法(它不太可能是一个相当大的加速,但仍然)。 -
找到了解决办法,贴在下面!
标签: java python performance collections