一篇关于HashMap性能的测试

具体代码如下:

public class MemoryLookupTest {
    public static void main(String[] args) throws IOException {
        String filePath = "D:/tmp_data/countData";
        LineNumberReader lineReader = new LineNumberReader(new InputStreamReader(new FileInputStream(filePath)));
        String line = null ;
        List<Recorder> recordList= new ArrayList<Recorder>();
        long start = System.currentTimeMillis() ;
        
        while((line = lineReader.readLine())!=null){
            String[] splits = line.split("    ");    
            Recorder r =new Recorder();
            r.acct_id = Long.parseLong(splits[0]);
            r.click = Long.parseLong(splits[2]);
            r.shw = Long.parseLong(splits[3]);
            recordList.add(r);
        }
        System.out.println(recordList.size()+" recorder ,cost is "+(System.currentTimeMillis()-start)+"ms");
        lineReader.close();
        Random random = new Random(1000);
        HashMap<Long,Long> map = new HashMap<Long,Long>();
        start = System.currentTimeMillis() ;
        for(Recorder r :recordList){
            map.put(r.acct_id, random.nextLong()/1000);
        }
        System.out.println(map.size()+" map initialize ,cost is "+(System.currentTimeMillis()-start)+"ms");
        HashMap<Long,Result> resultMap = new HashMap<Long,Result>();
        start = System.currentTimeMillis() ;
        for(Recorder r :recordList){
            Long manageId = map.get(r.acct_id);
            Result result = resultMap.get(manageId);
            if(result==null){
                result = new Result();
                resultMap.put(manageId, result);
            }
            result.click = result.click + r.click ;
            result.shw = result.shw + r.shw ;
        }
        System.out.println(resultMap.size()+" calculate ,cost is "+(System.currentTimeMillis()-start)+"ms");
    }
}
class Recorder{
    public long acct_id ;
    public long click ;
    public long shw ;
}
class Result{
    public long click  =0 ;
    public long shw = 0;
}

运行结果:

579319 recorder ,cost is 2635ms
308455 map initialize ,cost is 162ms
308455 calculate ,cost is 202ms

运行环境就是普通的笔记本,i5的cpu,8G的内存(但是这个程序使用的是默认虚拟机内存,应该是64M)

做的事情:

其实要做的事情,挺简单的,大约60W的一个list(存储的数据就是每个账户的点击展现数据),大约30W的一个hashMap(存储的是账户以及管理他的经理的id,在上面代码中,经理id是个随机构造的1000以下的数字),计算每个经理下的所有业绩,即每个经理管理的账户的总点击展现是多少 。

过程:遍历60W的list去,针对每条记录去hashMap中查找相应的经理id,然后累计相应的点击展现 。

结果:

结果如上面的展示,对于整个hashmap的查找其实还是挺快的,在30W记录的hashMap中查找60W次然后再相加,其耗时在200ms左右,其实还是相当快的  。

其实在单机的数据处理上,还是挺快的,百万的数据处理得当的话,完全可以在秒级别完成的 。

转载于:https://www.cnblogs.com/serendipity/articles/3861262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值