tyson記事本

Google & man(Unix) are your best friends!

Skip to: Content | Sidebar | Footer

JAVA 測試 String物件時間

12 January, 2007 (17:13) | JAVA | By: tyson

ref: http://www.javaworld.com.tw/jute/post/view?bid=29&id=9557

public class Test{
public static void main(String[] args){
new Test();
}

public Test(){
System.out.println("normal converting (ms): " + normal());
System.out.println("algorithm converting (ms): " + algo());
}

public long normal(){
int a = 0;
String b = null;
long st = System.currentTimeMillis();
for (int i = 0; i < 2000000; i++){
b = a + "";
}
long et = System.currentTimeMillis();
return (et - st);
}

public long algo(){
int a = 0;
String b = null;
long st = System.currentTimeMillis();
for (int i = 0; i < 2000000; i++){
b = Integer.toString(a);
}
long et = System.currentTimeMillis();
return (et - st);
}
}

output:
normal converting (ms): 2143
algorithm converting (ms): 701
—————————————
normal converting (ms): 2203
algorithm converting (ms): 671

Write a comment





For spam filtering purposes, please copy the number 8030 to the field below: