关于这期的coding4fun,我选择的是hashmap方式实现。整体思路和流程大家可能都差不多,C++同学们的总结写的很好,一些逻辑优化都有总结,我这里介绍下java实现的一些优化吧。
使用ByteString代替String
开始读出文件转成String对象,然后通过String对象操作,代码写起来都比较方便。
但是有一个问题,文件读取出来的byte[]转成String对象非常耗时,一个1G的String对象分配内存时间就很长了,String对象内部使用char[],通过byte[]构造String对象需要根据编码遍历byte[]。这个过程非常耗时,肯定是可以优化的。
于是我使用ByteString类代替String
class ByteString{
byte[] bs;
int start;
int end;
}
hashcode()和equals()方法参考String的实现。
在code4fun的16核机器上测试如下代码:
代码1:
byte[] bs = new byte[1024*1024*1024];
long st = System.currentTimeMillis();
new String(bs);
System.out.println(System.currentTimeMillis() - st); // 2619ms
代码2:
byte[] bs = new byte[1024*1024*1024];
long st = System.currentTimeMillis();
int count = 100000;
for(int i = 0; i < count; i++)
new ByteString(bs, 0, 100);
System.out.println(System.currentTimeMillis() - st); //10ms
循环中代码要精简
Hashmap的实现,给单词计数时避免不了如下的代码:
ByteString str = new ByteString(bs, start, end);
Count count = map.get(str);
If(count == null){
count = new Count(str,1);
map.put(str,count);
} else{
count.add(1);
}
本来这段代码没什么问题,但是当单词个数足够大的时候(最终1.1G的文件,有2亿多单词),这段代码就值得优化了。第一行创建的对象,只有单词第一次出现有用,其他时间都可以不用创建。
于是创建一个Pmap类,继承HahsMap,并添加了一个get(ByteStringbs,intstart,intend)方法。上面的代码改为
Count count = map.get(bs, start, end);
If(count == null){
ByteString str = new ByteString(bs, start, end);
count = new Count(str,1);
map.put(str,count);
} else{
count.add(1);
}
能避免锁就不用锁,不能避免就减小范围
concurrentHashMap的实现固然精妙,只是能不用锁尽量不用,实在用的时候,尽量减少范围。CAS的方式虽然比锁好,但是还是有消耗。
我们使用多线程的方式统计,所以统计结果对象需要线程安全。开始使用AtomicInteger,但是跟count++比起来效率还是差的非常多,单词个数越多越明显。
尝试使用volatile关键字效果也是不理想,然后比不上count++。