Skip to content

Instantly share code, notes, and snippets.

@melix
Created June 9, 2017 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save melix/dc96c3c36adfae63c3f0dabda2fe6e62 to your computer and use it in GitHub Desktop.
Save melix/dc96c3c36adfae63c3f0dabda2fe6e62 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()
@melix
Copy link
Author

melix commented Jun 9, 2017

Shows:

Environment
===========
* Groovy: 2.4.7
* JVM: Java HotSpot(TM) 64-Bit Server VM (25.91-b14, Oracle Corporation)
    * JRE: 1.8.0_91
    * Total Memory: 240 MB
    * Maximum Memory: 3545 MB
* OS: Linux (4.4.0-72-generic, amd64)

Options
=======
* Warm Up: Auto (- 60 sec)
* CPU Time Measurement: On

                    user  system   cpu  real

simple concat        730       9   739   739
gstring              564      12   576   576
gstring (closures)  1076       9  1085  1085
StringBuilder        218       3   221   221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment