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:100000, 1:100000, 2:100000]

耗时:0.266


class java.util.HashMap
[0:100000, 1:100000, 2:100000]
耗时:0.14

--------------------------------

当然,这不能算是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
3.985
==1==
100000
0
==2==
100000
0.016
==3==

虽然,Groovy提供了简洁的语法,但是使用起来,还是需要点技巧的。

我个人认为,Groovy需要在性能优化方面努努力。

转载于:https://my.oschina.net/tangcoffee/blog/207552

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值