Groovy I/O performance issue

groovy, performance

Solution

The left shift operator opens the file, jumps to the end, appends the text, and closes the file again...

Instead, try:

Random random = new Random();

// Open the file and append to it.
// If you want a new file each time, use withWriter instead of withWriterAppend
new File('sampledata.txt').withWriterAppend { w ->
  100000.times {
    w.writeLine "${new Date().format('yyyMMdd HH:mm:ss.SSS')}|box|process|||java.lang.Long|${random.nextInt(100)}|name"
  }
}

(this is also much more like what the Ruby code is doing)

Problem

I'm not a groovy expert, just use it from time to time. One of the latest goals was to generate a very simple file containing some random data. I created the following script: ``` out = new File('sampledata.txt') Random random = new Random(); java.util.Date dt = new java.util.Date(); for (int i=0; i<100000; ++i) { dt = new java.util.Date(); out << dt.format('yyyMMdd HH:mm:ss.SSS') + '|box|process|||java.lang.Long|' + random.nextInt(100) + '|name\n' } ``` Now, I'm really puzzled with its performance. It takes around 1.5 minutes to complete whilst the same code written in Java or Ruby takes less than a second. Similar code in Ruby (takes around 1 second to execute): require "time" ``` File.open("output.txt", "w") do |file| 100000.times do line = Time.now.strftime("%Y%m%d %H:%M:%S.%L") + '|box|process|||java.lang.Long|' + rand(100).to_s + '|name' file.puts line end end ``` Any ideas how groovy's processing speed could be improved?

Original source