java 如何实现计数_关于如何用java实现一个高效的计数器

最近在写毕设的时候遇到的一个,很常见的问题

就是对单词统计个数

关于如何高效的实现一般有下面几种方法:

[1]使用hashmap

但是注意不要使用containsKey(X) 来判断是否已经事先存在某个word 这会导致每次都遍历整个map

可以使用get(X)==null 来判断是否存在了该单词 这样更快

Integer count = map.get(word);

if(count == null){

count = 0;

}

map.put(word, count + 1);

[2]使用AtomicLong

final ConcurrentMap map =

new ConcurrentHashMap();

...

map.putIfAbsent(word, new AtomicLong(0));

map.get(word).incrementAndGet();

[3]使用trove【high perfomance for java collections】

TObjectIntHashMap freq = new TObjectIntHashMap();

...

freq.adjustOrPutValue(word, 1, 1);

[4]使用MutableInt

至于这个为什么会比HashMap 快的原因 还要慢慢寻找?

class MutableInt {

int value = 1; // note that we start at 1 since we're counting

public void increment () { ++value; }

public int get () { return value; }

}

...

Map freq = new HashMap();

...

MutableInt count = freq.get(word);

if (count == null) {

freq.put(word, new MutableInt());

}

else {

count.increment();

}

======结论:

ContainsKey: 30.654 seconds (baseline)

TestForNull:28.804 seconds (1.06 times as fast)

AtomicLong: 29.780 seconds (1.03 times as fast)

Trove: 26.313 seconds (1.16 times as fast)

MutableInt: 25.747 seconds (1.19 times as fast)

可以看见MutableInt 尽然是最快的(ps:apache commons组件实现了该类)还在思索中。。。。求高人解答(为何比)还要快?

下面可能是原因:理解中~

Memory rotation may be an issue here, since every boxing of an int larger than or equal to 128 causes an object allocation (see Integer.valueOf(int)). Although the garbage collector very efficiently deals with short-lived objects, performance will suffer to some degree.

If you know that the number of increments made will largely outnumber the number of keys (=words in this case), consider using an int holder instead. Phax already presented code for this. Here it is again, with two changes (holder class made static and initial value set to 1):

staticclassMutableInt{intvalue=1;voidinc(){++value;}intget(){returnvalue;}}...Mapmap=newHashMap();MutableIntvalue=map.get(key);if(value==null){value=newMutableInt();map.put(key,value);}else{value.inc();}

If you need extreme performance, look for a Map implementation which is directly tailored towards primitive value types. jrudolph mentioned GNU Trove.

By the way, a good search term for this subject is "histogram".

======

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值