Skip to content

Instantly share code, notes, and snippets.

@ybon3
Last active October 18, 2016 02:15
Show Gist options
  • Save ybon3/5ae0f8cb2f5e39247f3e2c6c3e8b6f19 to your computer and use it in GitHub Desktop.
Save ybon3/5ae0f8cb2f5e39247f3e2c6c3e8b6f19 to your computer and use it in GitHub Desktop.
用來測試 GWT 於 client side 處理 Try-Catch vs If-Else 的效率

Catch Exception vs If-Else

  • Catch Exception -> 簡稱 A
  • If-Else -> 簡稱 B
  • 資料筆數:近 500 筆
  • Browser:Chrome,版本 53.0.2785.143 m (64-bit)
  1. 早前用 vp 的情況

    • 執行次數:500 * 5 * 500(loop)
    • 不炸任何 Exception
    • 結果:雙方持平(約 46 秒)
  2. 使用純 function 進行,type 1

    • 執行次數:500 * 5 * 5000(loop)
    • 不炸任何 Exception
    • 結果:A 比較快,約快了 100 ~ 200 ms
  3. 使用純 function 進行,type 2

    • 執行次數:500 * 5 * 50(loop)
    • 在 getBar() 炸 NPE (第二層)
    • 結果:B 比較快,快很多 (13 : 7xxx)
  4. 使用純 function 進行,type 3

    • 執行次數:500 * 5 * 50(loop)
    • 在 getFoo() 炸 NPE (第一層)
    • 結果:B 比較快,快很多 (3 : 7xxx)
package com.dtc.test.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer.HorizontalLayoutData;
import com.sencha.gxt.widget.core.client.container.Viewport;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
public class TestEP implements EntryPoint {
// 用來表現一般情況的
public static String getValueNormal(FooBar object) {
if(object.getFoo() == null) return "";
if(object.getFoo().getBar() == null) return "boom!!";
return object.getFoo().getBar().getValue();
}
// 用來測試 try-catch style
public static String getValueTryCatch(FooBar object) {
try {return object.getFoo().getBar().getValue();}
catch (Exception e) {return "boom!";}
}
private static TextButton btnNormal = new TextButton("Normal");
private static TextButton btnTryCat = new TextButton("TryCatch");
static {
btnNormal.addSelectHandler(new SelectHandler(){
@Override
public void onSelect(SelectEvent event) {
long cnt = System.currentTimeMillis();
for (int i = 0; i < 50; i++) {
for (FooBar data : TestData.DATA) {
getValueNormal(data);
getValueNormal(data);
getValueNormal(data);
getValueNormal(data);
getValueNormal(data);
}
}
GWT.log("[NORMAL] used time millis: " + (System.currentTimeMillis() - cnt));
}
});
btnTryCat.addSelectHandler(new SelectHandler(){
@Override
public void onSelect(SelectEvent event) {
long cnt = System.currentTimeMillis();
for (int i = 0; i < 50; i++) {
for (FooBar data : TestData.DATA) {
getValueTryCatch(data);
getValueTryCatch(data);
getValueTryCatch(data);
getValueTryCatch(data);
getValueTryCatch(data);
}
}
GWT.log("[TRY CATCH] used time millis: " + (System.currentTimeMillis() - cnt));
}
});
}
@Override
public void onModuleLoad() {
Viewport vp = new Viewport();
HorizontalLayoutContainer hlc = new HorizontalLayoutContainer();
HorizontalLayoutData hld = new HorizontalLayoutData(0.5, 1);
hlc.add(btnNormal, hld);
hlc.add(btnTryCat, hld);
vp.add(hlc);
RootPanel.get().add(vp);
}
}
class FooBar {
private String id;
private Foo foo;
public FooBar(String id, String value) {
this.id = id;
this.foo = new Foo(value);
}
public Foo getFoo() { return null; } //foo; }
public String getId() { return id; }
class Foo {
private Bar bar;
public Foo(String value) { this.bar = new Bar(value); }
public Bar getBar() { return null; }// bar; }
class Bar {
private String value;
public Bar(String value) { this.value = value; }
public String getValue() { return value; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment