Groovy,革命尚未成功,同志仍需努力。
公司要搞一个员工之间的技术交流,每个人准备一个关注的课题。 本喵,打算准备的课题是:jvm上的几种script语言实现。? Ruby、Python、Scala、Groovy等。 于是今天对Groovy做了几个简单的性能测试。 static?def?t4(){ ????????def?o?=?null ????????def?t?=?System.currentTimeMillis(); ????????for(i?in?0..100000){ ????????????o?=?[0:i,?1:i,?2:i] ????????} ????????println?o.getClass() ????????println?o ????????def?ht?=?System.currentTimeMillis()-t ????????println("耗时:${ht/1000}") ????????Map<Integer,Integer>?o1?=?null ????????long?t1?=?System.currentTimeMillis() ????????for(int?i=0;?i<=100000;?i++){ ??????????o1?=?new?HashMap<Integer,Integer>() ??????????o1.put(0,i) ??????????o1.put(1,i) ??????????o1.put(2,i) ????????} ????????System.out.println(o1.getClass()) ????????System.out.println(o1) ????????long?ht1?=?System.currentTimeMillis()-t1 ????????System.out.println("耗时:"+ht1/1000) ????} 反复执行了几遍,结果差不多: class java.util.LinkedHashMap 耗时:0.266
-------------------------------- 当然,这不能算是Groovy的性能问题,Groovy采用的是java的实现,而map实现是LinkedHashMap,这种有序的Map不可能比HashMap快。只是我不明白,Groovy为啥用LinkedHashMap,我个人认为大部分情况下,不需要有序的Map,Groovy何必多此一举? static?def?t5(){ ????????int?n?=?100*100 ????????def?ml?=?''''''??????? ????????long?t?=?System.currentTimeMillis() ????????for(i?in?1..n){ ????????????ml?+=?"1234567890" ????????????//ml?<<?"1234567890" ????????} ????????long?ht?=?System.currentTimeMillis()-t ????????System.out.println(ml.length()) ????????System.out.println(ht/1000) ????????System.out.println("==1==") ????????def?ms?=?new?StringBuilder() ????????t?=?System.currentTimeMillis() ????????for(i?in?1..n){ ????????????//ms?+=?"1234567890"?//跟ml?+=?"1234567890"差不多慢 ????????????ms?<<?"1234567890" ????????} ????????ht?=?System.currentTimeMillis()-t ????????System.out.println(ms.length()) ????????System.out.println(ht/1000) ????????System.out.println("==2==") ????????t?=?System.currentTimeMillis() ????????StringBuilder?sb?=?new?StringBuilder() ????????for(int?i=1;?i<=n;?i++){ ????????????sb.append("1234567890") ????????} ????????ht?=?System.currentTimeMillis()-t ????????System.out.println(sb.length()) ????????System.out.println(ht/1000) ????????System.out.println("==3==") ????} 这个测试的结果,我想大家猜到了: 100000 虽然,Groovy提供了简洁的语法,但是使用起来,还是需要点技巧的。 我个人认为,Groovy需要在性能优化方面努努力。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |