1 25 40 package org.jgrapht.demo; 41 42 import java.io.*; 43 44 import java.util.*; 45 46 import org.jgrapht.*; 47 import org.jgrapht.graph.*; 48 import org.jgrapht.traverse.*; 49 50 51 67 public final class PerformanceDemo 68 { 69 70 72 77 public static void main(String [] args) 78 { 79 long time = System.currentTimeMillis(); 80 81 reportPerformanceFor("starting at", time); 82 83 Graph<Object , DefaultEdge> g = 84 new Pseudograph<Object , DefaultEdge>(DefaultEdge.class); 85 Object prev; 86 Object curr; 87 88 curr = prev = new Object (); 89 g.addVertex(prev); 90 91 int numVertices = 10000; 92 int numEdgesPerVertex = 200; 93 int numElements = numVertices * (1 + numEdgesPerVertex); 94 95 System.out.println( 96 "\n" + "allocating graph with " + numElements 97 + " elements (may take a few tens of seconds)..."); 98 99 for (int i = 0; i < numVertices; i++) { 100 curr = new Object (); 101 g.addVertex(curr); 102 103 for (int j = 0; j < numEdgesPerVertex; j++) { 104 g.addEdge(prev, curr); 105 } 106 107 prev = curr; 108 } 109 110 reportPerformanceFor("graph allocation", time); 111 112 time = System.currentTimeMillis(); 113 114 for ( 115 Iterator i = new BreadthFirstIterator<Object , DefaultEdge>(g); 116 i.hasNext();) { 117 i.next(); 118 } 119 120 reportPerformanceFor("breadth traversal", time); 121 122 time = System.currentTimeMillis(); 123 124 for ( 125 Iterator i = new DepthFirstIterator<Object , DefaultEdge>(g); 126 i.hasNext();) { 127 i.next(); 128 } 129 130 reportPerformanceFor("depth traversal", time); 131 132 System.out.println( 133 "\n" 134 + "Paused: graph is still in memory (to check mem consumption)."); 135 System.out.print("press any key to free memory and finish..."); 136 137 try { 138 System.in.read(); 139 } catch (IOException e) { 140 e.printStackTrace(); 141 } 142 143 System.out.println("done."); 144 } 145 146 private static void reportPerformanceFor(String msg, long refTime) 147 { 148 double time = (System.currentTimeMillis() - refTime) / 1000.0; 149 double mem = usedMemory() 150 / (1024.0 * 1024.0); 151 mem = Math.round(mem * 100) / 100.0; 152 System.out.println(msg + " (" + time + " sec, " + mem + "MB)"); 153 } 154 155 private static long usedMemory() 156 { 157 Runtime rt = Runtime.getRuntime(); 158 159 return rt.totalMemory() - rt.freeMemory(); 160 } 161 } 162 | Popular Tags |