我发现在下面的上下文中使用的Arrays.sort方法中运行时间的变化很大。我想知道这是否是一个常见问题,或者如果我编写代码的方式效率低下?可变性是,在100,000,000个节点的图表上的某些运行中,一次运行的运行时间大约为10秒,而在具有相同线程数的同一图表上的另一次运行则为270秒。
编辑:我担心的是,当我运行时通常代码需要100-300秒,但偶尔运行只需要10-20秒。例如,使用30个线程(在40核计算机上)运行100,000,000个节点图,我一次运行150秒,然后再运行20秒。机器上没有任何变化(我使用top来确保没有其他人可以使用它),所以我不确定是什么原因造成的。
public class AdjacencyGraph {
static Comparator<Node> comp = new Comparator<Node>(){
@Override
public int compare(Node n1,Node n2){
return n1.index - n2.index;
}
};
static class Node {
final int index;
Node[] neighbors;
boolean inMIS = false;
// public final ReadWriteLock lock = new ReentrantReadWriteLock();
Node(int index) {
super();
{
}
{
}
{
}
this.index = index;
}
void initNeighbors(int degree) {
neighbors = new Node[degree];
}
}
public static void sort_neighbors(Node n)
{
Arrays.sort(n.neighbors,comp);
}
Node[] nodes;
public AdjacencyGraph(int n) {
super();
{
}
{
}
{
}
nodes = new Node[n];
}
public static void sort_neighbors(Node n)
{
Arrays.sort(n.neighbors,comp);
}
static AdjacencyGraph readAdjacencyGraph(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
if (!"AdjacencyGraph".equals(reader.readLine())) {
throw new IOException("invalid adjacency graph format");
}
int n = Integer.parseInt(reader.readLine());
int m = Integer.parseInt(reader.readLine());
int[] offsets = new int[n];
int[] edges = new int[m];
for (int i = 0; i < n; i++) {
offsets[i] = Integer.parseInt(reader.readLine());
}
for (int i = 0; i < m; i++) {
edges[i] = Integer.parseInt(reader.readLine());
}
final AdjacencyGraph g = new AdjacencyGraph(n);
for (int i = 0; i < n; i++) {
final Node node = new Node(i);
g.nodes[i] = node;
}
for (int i = 0; i < n; i++) {
int startOffset = offsets[i];
int endOffset = (i == n - 1) ? m : offsets[i + 1];
g.nodes[i].initNeighbors(endOffset - startOffset);
for (int j = startOffset; j < endOffset; j++) {
g.nodes[i].neighbors[j - startOffset] = g.nodes[edges[j]];
}
}
return g;
}
}
}
下一课:
public class GraphSortRunnable implements Runnable
{
private final int start_idx;
private final int end_idx;
private AdjacencyGraph.Node[] nodes;
public GraphSortRunnable(int start, int end, AdjacencyGraph.Node[] nodes)
{
this.start_idx = start;
this.end_idx = end;
this.nodes = nodes;
}
public void run()
{
for(int i = this.start_idx; i < this.end_idx; i++)
AdjacencyGraph.sort_neighbors(nodes[i]);
}
}
主要方法:
public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException
{
AdjacencyGraph g = AdjacencyGraph.readAdjacencyGraph(args[0]);
final int NUM_THREADS = Integer.parseInt(args[1]);
int size = g.nodes.length / NUM_THREADS;
List<Thread> threads_sort = new ArrayList<Thread>();
final long start_sort = System.currentTimeMillis();
for(int i = 0; i < NUM_THREADS; i++)
{
Runnable task = new GraphSortRunnable(i*size, (i+1)*size, g.nodes);
Thread worker = new Thread(task);
worker.setName(String.valueOf(i));
worker.start();
threads_sort.add(worker);
}
for(int i = 0; i < NUM_THREADS; i++)
threads_sort.get(i).join();
final long end_sort = System.currentTimeMillis();
System.out.println("sort time: " + (end_sort - start_sort)+ "milliseconds");
// do additional work
}