---
title: "Groovy-Java性能对比"
author: "key232323"
date: "2015-11-16T13:22:53Z"
tags: ["Groovy", "性能"]
categories: ["编程语言"]
slug: "groovy-java"
draft: false
lastmod: "2015-11-16T13:22:53Z"
id: 2257044
url: "https://key232323.iteye.com/blog/2257044"
---

摘要：Groovy用@CompileStatic性能和Java一样，Script比Java慢一个数量级。 在业务方法占主要消耗时间的情况下，均无区别。

 

先说结论，如上面摘要所述，用Groovy的语法糖的同时如果苛求性能，就加@CompileStatic编译注解。

下面是用punit做的2次并发性能测试结果，第一次是执行一个简单的方法调用，第二个是模拟一个耗时的业务方法。

工程代码，请参考我的git：

http://git.oschina.net/key232323/groovy-performance-test

 

第一次：（只做计算）

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[0.007931ms]

testGroovyInterfaceAndStatic() - \[0.006531ms]

testGroovyInterface() - \[0.078376ms]

testGroovyScript() - \[0.099371ms]

total: 4, failures:0 (GREEN) - 919.502066ms

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[0.006065ms]

testGroovyInterfaceAndStatic() - \[0.007464ms]

testGroovyInterface() - \[0.078376ms]

testGroovyScript() - \[0.106368ms]

total: 4, failures:0 (GREEN) - 85.572821ms

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[0.007931ms]

testGroovyInterfaceAndStatic() - \[0.005598ms]

testGroovyInterface() - \[0.079777ms]

testGroovyScript() - \[0.091906ms]

total: 4, failures:0 (GREEN) - 500.078843ms

 

第二次：（Sleep 10ms模拟耗时的业务方法）

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[10053.493918ms]

testGroovyInterfaceAndStatic() - \[10041.729026ms]

testGroovyInterface() - \[10185.924769ms]

testGroovyScript() - \[10039.974882ms]

total: 4, failures:0 (GREEN) - 41040.644815ms

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[10058.807668ms]

testGroovyInterfaceAndStatic() - \[10050.167577ms]

testGroovyInterface() - \[10063.309659ms]

testGroovyScript() - \[10067.116524ms]

total: 4, failures:0 (GREEN) - 40332.292666ms

\[concurrent] Starting com.innohub.bench.runner.Tester

com.innohub.bench.runner.Tester

testDoCalJava() - \[10028.900451ms]

testGroovyInterfaceAndStatic() - \[10028.953634ms]

testGroovyInterface() - \[10042.517924ms]

testGroovyScript() - \[10037.078679ms]

total: 4, failures:0 (GREEN) - 40468.62882ms