KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > demo > PerformanceDemo


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* --------------------
26  * PerformanceDemo.java
27  * --------------------
28  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
29  *
30  * Original Author: Barak Naveh
31  * Contributor(s): -
32  *
33  * $Id: PerformanceDemo.java 504 2006-07-03 02:37:26Z perfecthash $
34  *
35  * Changes
36  * -------
37  * 10-Aug-2003 : Initial revision (BN);
38  *
39  */

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 /**
52  * A simple demo to test memory and CPU consumption on a graph with 3 million
53  * elements.
54  *
55  * <p>NOTE: To run this demo you may need to increase the JVM max mem size. In
56  * Sun's JVM it is done using the "-Xmx" switch. Specify "-Xmx300M" to set it to
57  * 300MB.</p>
58  *
59  * <p>WARNING: Don't run this demo as-is on machines with less than 512MB
60  * memory. Your machine will start paging severely. You need to first modify it
61  * to have fewer graph elements. This is easily done by changing the loop
62  * counters below.</p>
63  *
64  * @author Barak Naveh
65  * @since Aug 10, 2003
66  */

67 public final class PerformanceDemo
68 {
69
70     //~ Methods ---------------------------------------------------------------
71

72     /**
73      * The starting point for the demo.
74      *
75      * @param args ignored.
76      */

77     public static void main(String JavaDoc [] args)
78     {
79         long time = System.currentTimeMillis();
80
81         reportPerformanceFor("starting at", time);
82
83         Graph<Object JavaDoc, DefaultEdge> g =
84             new Pseudograph<Object JavaDoc, DefaultEdge>(DefaultEdge.class);
85         Object JavaDoc prev;
86         Object JavaDoc curr;
87
88         curr = prev = new Object JavaDoc();
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 JavaDoc();
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 JavaDoc, 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 JavaDoc, 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 JavaDoc 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 JavaDoc rt = Runtime.getRuntime();
158
159         return rt.totalMemory() - rt.freeMemory();
160     }
161 }
162
Popular Tags