Java高效计数器(二)
源字符串
String source = "my name is name me and your name is her first her";
// 计时,单位: 微秒
long startTime = 0;
long endTime = 0;
long duration = 0;
// 测试次数
int loop = 1 * 10000;
System.out.println(loop +" 次循环:");
startTime = System.nanoTime();
testNaive(source,loop);
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("新手级计数器: " + duration);
//
startTime = System.nanoTime();
testBetter(source, loop);
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("入门级计数器: " + duration);
//
startTime = System.nanoTime();
testEfficient(source, loop);
endTime = System.nanoTime();
duration = endTime - startTime;
System.out.println("卓越级计数器: " + duration);
}
// 新手级计数器
public static void testNaive(String source, int loop){
if(null == source){
return;
}
//
String[] words = source.split(" ");
for (int i = 0; i < loop; i++) {
testNaive(words);
}
}
public static void testNaive(String[] words){
HashMap counter = new HashMap();
for (String w : words) {
if(counter.containsKey(w)){
int oldValue = counter.get(w);
counter.put(w, oldValue+1);
} else {
counter.put(w, 1);
}
}
}
// 可变Integer
public static final class MutableInteger{
private int val;
public MutableInteger(int val){
this.val = val;
}
public int get(){
return this.val;
}
public void set(int val){
this.val = val;
}
// 为了方便打印
public String toString() {
return Integer.toString(val);
}
}
// 入门级计数器
public static void testBetter(String source, int loop){
if(null == source){
return;
}
//
String[] words = source.split(" ");
for (int i = 0; i < loop; i++) {
testBetter(words);
}
}
public static void testBetter(String[] words){
HashMap counter = new HashMap();
for (String w : words) {
if(counter.containsKey(w)){
MutableInteger oldValue = counter.get(w);
oldValue.set(oldValue.get()+1); // 因为是引用,所以减少了一次HashMap查找
} else {
counter.put(w, new MutableInteger(1));
}
}
}
// 卓越级计数器
public static void testEfficient(String source, int loop){
if(null == source){
return;
}
//
String[] words = source.split(" ");
for (int i = 0; i < loop; i++) {
testEfficient(words);
}
}
public static void testEfficient(String[] words){
HashMap counter = new HashMap();
for (String w : words) {
MutableInteger initValue = new MutableInteger(1);
// 利用 HashMap 的put方法弹出旧值的特性
MutableInteger oldValue = counter.put(w, initValue);
if(oldValue != null){
initValue.set(oldValue.get() + 1);
}
}
}
}
当你实用计数器的时候,很可能也需要根据值来进行排序的方法,请参考: the frequently used method of HashMap.