翻看别人的测试Groovy文章引起灵感,测试了一下Groovy中三种Loop的性能
- def testForIn(){
- def start = System.currentTimeMillis();
- int i=0
- for (int x in 1..10000) {
- for (int y in 1..1000) {
- //i+=y
- }
- }
- println System.currentTimeMillis() - start
- println i
- }
- def testRangeEach(){
- def start = System.currentTimeMillis();
- int i=0
- (1..10000).each{x->
- (1..1000).each{y->
- // i+=y
- }
- }
- println System.currentTimeMillis() - start
- println i
- }
- def testOldJavaLoop(){
- def start = System.currentTimeMillis();
- int i=0
- for(int x= 1;x<=10000;x++) {
- for(int y=1;y<=1000;y++) {
- // i=i+y;
- }
- }
- println System.currentTimeMillis() - start
- println i
- }
- testForIn()
- testRangeEach()
- testOldJavaLoop()
结果:
563
0
3469
0
4109
0