From fbc86ceae5b187c99180cd680f89af55341e4133 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Sat, 9 Apr 2011 08:07:28 +0000 Subject: [PATCH] HashMap versus JsMap/JsCache --- .../JsCollectionVsJavaCollection.gwt.xml | 7 + .../client/JsCollectionVsJavaCollection.java | 155 ++++++++++++++++++ .../public/JsCollectionVsJavaCollection.html | 9 + 3 files changed, 171 insertions(+) create mode 100644 samples/src/main/java/gwtquery/samples/JsCollectionVsJavaCollection.gwt.xml create mode 100644 samples/src/main/java/gwtquery/samples/client/JsCollectionVsJavaCollection.java create mode 100644 samples/src/main/java/gwtquery/samples/public/JsCollectionVsJavaCollection.html diff --git a/samples/src/main/java/gwtquery/samples/JsCollectionVsJavaCollection.gwt.xml b/samples/src/main/java/gwtquery/samples/JsCollectionVsJavaCollection.gwt.xml new file mode 100644 index 00000000..3011cca5 --- /dev/null +++ b/samples/src/main/java/gwtquery/samples/JsCollectionVsJavaCollection.gwt.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/samples/src/main/java/gwtquery/samples/client/JsCollectionVsJavaCollection.java b/samples/src/main/java/gwtquery/samples/client/JsCollectionVsJavaCollection.java new file mode 100644 index 00000000..14c06375 --- /dev/null +++ b/samples/src/main/java/gwtquery/samples/client/JsCollectionVsJavaCollection.java @@ -0,0 +1,155 @@ +package gwtquery.samples.client; + +import static com.google.gwt.query.client.GQuery.$; + +import com.google.gwt.core.client.EntryPoint; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.query.client.js.JsCache; +import com.google.gwt.query.client.js.JsMap; +import com.google.gwt.user.client.Random; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.RootPanel; + +import java.util.HashMap; + +public class JsCollectionVsJavaCollection implements EntryPoint{ + + public static final int MAX_ITEMS = 100000; + + + public void onModuleLoad() { + + Button b = new Button("run test"); + b.addClickHandler(new ClickHandler() { + + public void onClick(ClickEvent event) { + $(".gwt-label").remove(); + testJsMapVsHashMap(); + } + }); + + RootPanel.get().add(b); + + } + + + public void testJsMapVsHashMap() { + log("Testing jsMap"); + + log("init phase"); + for (int i = 0; i < MAX_ITEMS; i++){ + new String(""+i); + } + + JsCache cache = JsCache.create(); + JsMap jsMap = cache.cast(); + + log("Testing jsMap : put "+MAX_ITEMS+" items in the map :"); + + long ellapsedTime = System.currentTimeMillis(); + for (int i = 0; i < MAX_ITEMS; i++){ + jsMap.put(new String(""+i), new Object()); + } + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("Testing jsMap : do "+MAX_ITEMS+" get in the map :"); + long totalTime = 0; + + for (int i = 0 ; i < MAX_ITEMS; i++){ + int random = Random.nextInt(MAX_ITEMS); + ellapsedTime = System.currentTimeMillis(); + jsMap.get(new String(""+random)); + totalTime += (System.currentTimeMillis() - ellapsedTime); + } + log(" ellapsed Time : "+totalTime); + + log("Testing jsMap : do "+MAX_ITEMS+" exist (JsCache.exists() ) in the cache :"); + totalTime = 0; + + for (int i = 0 ; i < MAX_ITEMS; i++){ + int random = Random.nextInt(MAX_ITEMS); + ellapsedTime = System.currentTimeMillis(); + cache.exists(new String(""+random)); + totalTime += (System.currentTimeMillis() - ellapsedTime); + } + log(" ellapsed Time : "+totalTime); + + log("Testing jsMap : get all keys :"); + + ellapsedTime = System.currentTimeMillis(); + jsMap.keys(); + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("Testing jsMap : get all values (JsCache.elements() ) :"); + + ellapsedTime = System.currentTimeMillis(); + cache.elements(); + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("-------------"); + log(""); + + HashMap hashMap = new HashMap(); + + log("Testing hashMap : put "+MAX_ITEMS+" items in the map :"); + ellapsedTime = System.currentTimeMillis(); + for (int i = 0; i < MAX_ITEMS; i++){ + hashMap.put(new String(""+i), new Object()); + } + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("Testing hashMap : do "+MAX_ITEMS+" get in the map :"); + totalTime = 0; + + for (int i = 0 ; i < MAX_ITEMS; i++){ + int random = Random.nextInt(MAX_ITEMS); + ellapsedTime = System.currentTimeMillis(); + hashMap.get(new String(""+random)); + totalTime += (System.currentTimeMillis() - ellapsedTime); + } + log(" ellapsed Time : "+totalTime); + + log("Testing hashMap : do "+MAX_ITEMS+" containsKey() :"); + totalTime = 0; + + for (int i = 0 ; i < MAX_ITEMS; i++){ + int random = Random.nextInt(MAX_ITEMS); + ellapsedTime = System.currentTimeMillis(); + hashMap.containsKey(new String(""+random)); + totalTime += (System.currentTimeMillis() - ellapsedTime); + } + log(" ellapsed Time : "+totalTime); + + log("Testing hashMap : get all keys :"); + + ellapsedTime = System.currentTimeMillis(); + hashMap.keySet(); + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("Testing hashMap : get all values :"); + + ellapsedTime = System.currentTimeMillis(); + hashMap.values(); + ellapsedTime = System.currentTimeMillis() - ellapsedTime; + log(" ellapsed Time : "+ellapsedTime); + + log("-------------"); + + } + + + + + public void log(String msg) { + RootPanel.get().add(new Label(msg)); + } + + +} diff --git a/samples/src/main/java/gwtquery/samples/public/JsCollectionVsJavaCollection.html b/samples/src/main/java/gwtquery/samples/public/JsCollectionVsJavaCollection.html new file mode 100644 index 00000000..32b9c535 --- /dev/null +++ b/samples/src/main/java/gwtquery/samples/public/JsCollectionVsJavaCollection.html @@ -0,0 +1,9 @@ + + +GQuery Demo + + + + + -- 2.39.5