Skip to content

Instantly share code, notes, and snippets.

@daniellansun
Forked from melix/bench-string-concat.groovy
Created November 30, 2017 00:29
Show Gist options
  • Save daniellansun/565be52ee59b325324c1752dd8e44414 to your computer and use it in GitHub Desktop.
Save daniellansun/565be52ee59b325324c1752dd8e44414 to your computer and use it in GitHub Desktop.
String concat Groovy
@Grab(group='org.gperfutils', module='gbench', version='0.4.3-groovy-2.4')
String a = 'The quick brown fox'
String b = 'jumps over the lazy dog'
int x = 1
double y = 2
benchmark {
'simple concat' {
String concat = a + ' ' + b + ' ' + x + ' ' + y
}
'gstring' {
String concat = "$a $b $x $y"
}
'gstring (closures)' {
String concat = "${-> a} ${-> b} ${-> x} ${-> y}"
}
'StringBuilder' {
String concat = new StringBuilder()
.append(a).append(' ').append(b).append(' ').append(x).append(' ').append(y)
}
}.prettyPrint()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment