Created
February 12, 2018 15:45
-
-
Save marcgeld/832472afb31221be0c27e54f67725282 to your computer and use it in GitHub Desktop.
Micro-benchmarking in Groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env groovy | |
| @Grab(group='com.googlecode.gbench', module='gbench', version='0.4.2-groovy-2.3') | |
| import groovyx.gbench.BenchmarkBuilder | |
| def charGen = { String alphabet, int n -> | |
| new Random().with { | |
| (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join() | |
| } | |
| } | |
| (0..512).step(2048).each { size -> | |
| println "Test with string size: ${size}" | |
| def (a,b,c,d,e,f) = [ | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size), // a | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size), // b | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size), // c | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size), // d | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size), // e | |
| charGen( (('A'..'Z')+('0'..'9')).join(), size) // f | |
| ] | |
| (new BenchmarkBuilder()).run(verbose: true, maxWarmUpTime: 1 /* ,measureCpuTime: false*/ ){ | |
| "String adder-${size}b" { | |
| a + b + c + d + e + f | |
| } | |
| "GString template-${size}b" { | |
| "$a$b$c$d$e" | |
| } | |
| "Readable GString template-${size}b" { | |
| "${a}${b}${c}${d}${e}" | |
| } | |
| "StringBuilder-${size}b" { | |
| new StringBuilder().append( a ) | |
| .append( b ) | |
| .append( c ) | |
| .append( d ) | |
| .append( e ) | |
| .toString() | |
| } | |
| "StringBuffer--${size}b" { | |
| new StringBuffer().append( a ) | |
| .append( b ) | |
| .append( c ) | |
| .append( d ) | |
| .append( e ) | |
| .toString() | |
| } | |
| }.prettyPrint() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment