daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Feb 24th, 2018 64 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #!/usr/bin/env groovy
  2.  
  3.  @Grab(group='com.googlecode.gbench', module='gbench', version='0.4.2-groovy-2.3')
  4.  
  5. import groovyx.gbench.BenchmarkBuilder
  6.  
  7.  
  8. def charGen = { String alphabet, int n ->
  9.   new Random().with {
  10.     (1..n).collect { alphabet[ nextInt( alphabet.length() ) ] }.join()
  11.   }
  12. }
  13.  
  14. (0..512).step(2048).each { size ->
  15.  
  16.   println "Test with string size: ${size}"
  17.  
  18.   def (a,b,c,d,e,f) = [
  19.  
  20.     charGen( (('A'..'Z')+('0'..'9')).join(), size), // a
  21.     charGen( (('A'..'Z')+('0'..'9')).join(), size), // b
  22.     charGen( (('A'..'Z')+('0'..'9')).join(), size), // c
  23.     charGen( (('A'..'Z')+('0'..'9')).join(), size), // d
  24.     charGen( (('A'..'Z')+('0'..'9')).join(), size), // e
  25.     charGen( (('A'..'Z')+('0'..'9')).join(), size) // f
  26.   ]
  27.  
  28.   (new BenchmarkBuilder()).run(verbose: true, maxWarmUpTime: 1 /* ,measureCpuTime: false*/ ){
  29.  
  30.     "String adder-${size}b" {
  31.       a + b + c + d + e + f
  32.     }
  33.  
  34.     "GString template-${size}b" {
  35.       "$a$b$c$d$e"
  36.     }
  37.  
  38.     "Readable GString template-${size}b" {
  39.       "${a}${b}${c}${d}${e}"
  40.     }
  41.  
  42.     "StringBuilder-${size}b" {
  43.       new StringBuilder().append( a )
  44.                          .append( b )
  45.                          .append( c )
  46.                          .append( d )
  47.                          .append( e )
  48.                          .toString()
  49.     }
  50.    
  51.     "StringBuffer--${size}b" {
  52.       new StringBuffer().append( a )
  53.                          .append( b )
  54.                          .append( c )
  55.                          .append( d )
  56.                          .append( e )
  57.                          .toString()
  58.     }
  59.   }.prettyPrint()
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top