| NodeMapTest.java |
1 /****************************************************************************
2 **
3 ** This file is part of yFiles-2.9.
4 **
5 ** yWorks proprietary/confidential. Use is subject to license terms.
6 **
7 ** Redistribution of this file or of an unauthorized byte-code version
8 ** of this file is strictly forbidden.
9 **
10 ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28,
11 ** 72070 Tuebingen, Germany. All rights reserved.
12 **
13 ***************************************************************************/
14 package demo.base;
15
16 import java.util.HashMap;
17 import java.util.Map;
18 import y.util.Maps;
19 import y.util.Timer;
20 import y.util.D;
21 import y.base.Graph;
22 import y.base.NodeMap;
23 import y.base.NodeCursor;
24 import y.base.Node;
25
26
27 /**
28 * This class compares the performance of different
29 * mechanisms to bind extra data to the nodes of a graph.
30 * In scenarios where the indices of the nodes do not change
31 * while the extra node data is needed, it is best to use array based
32 * mechanisms that use the index of a node to access the data.
33 * <br>
34 * In scenarios where the indices of the nodes will change
35 * while the extra node data is needed, it is necessary to
36 * use {@link y.base.NodeMap} implementations that do not depend on the indices
37 * of the nodes (see {@link Node#index()}) or {@link java.util.Map}
38 * implementations like {@link java.util.HashMap} provided by the java
39 * collections framework.
40 *
41 * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/data_accessors.html#Maps and Data Providers">Section Maps and Data Providers</a> in the yFiles for Java Developer's Guide
42 */
43 public class NodeMapTest
44 {
45
46 static Timer t1 = new Timer(false);
47 static Timer t2 = new Timer(false);
48 static Timer t3 = new Timer(false);
49 static Timer t4 = new Timer(false);
50 static Timer t5 = new Timer(false);
51
52 public static void main(String[] args)
53 {
54 test1();
55 }
56
57 static void test1()
58 {
59 Graph graph = new Graph();
60 for(int i = 0; i < 20000; i++) graph.createNode();
61
62 for(int loop = 0; loop < 10; loop++)
63 {
64 D.bu(".");
65
66 t1.start();
67 NodeMap map = graph.createNodeMap();
68 for(int i = 0; i < 10; i++)
69 {
70 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
71 {
72 Node v = nc.node();
73 map.setInt(v,i);
74 i = map.getInt(v);
75 }
76 }
77 graph.disposeNodeMap(map);
78 t1.stop();
79
80
81 t2.start();
82 map = Maps.createIndexNodeMap(new int[graph.N()]);
83 for(int i = 0; i < 10; i++)
84 {
85 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
86 {
87 Node v = nc.node();
88 map.setInt(v,i);
89 map.getInt(v);
90 }
91 }
92 t2.stop();
93
94
95 t3.start();
96 map = Maps.createHashedNodeMap();
97 for(int i = 0; i < 10; i++)
98 {
99 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
100 {
101 Node v = nc.node();
102 map.setInt(v, i);
103 i = map.getInt(v);
104 }
105 }
106 t3.stop();
107
108 t4.start();
109 int[] array = new int[graph.N()];
110 for(int i = 0; i < 10; i++)
111 {
112 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
113 {
114 int vid = nc.node().index();
115 array[vid] = i;
116 i = array[vid];
117 }
118 }
119 t4.stop();
120
121
122 t5.start();
123 Map jmap = new HashMap(2*graph.N()+1); //use hash map with good initial size
124 for(int i = 0; i < 10; i++)
125 {
126 for(NodeCursor nc = graph.nodes(); nc.ok(); nc.next())
127 {
128 Node v = nc.node();
129 jmap.put(v, new Integer(i));
130 i = ((Integer)jmap.get(v)).intValue();
131 }
132 }
133 t5.stop();
134
135 }
136
137 D.bug("");
138 D.bug("TIME: standard NodeMap: " + t1);
139 D.bug("TIME: index NodeMap: " + t2);
140 D.bug("TIME: hashed NodeMap: " + t3);
141 D.bug("TIME: plain array : " + t4);
142 D.bug("TIME: HashMap : " + t5);
143 }
144 }
145
146
147
148
149