Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy I/O performance issue

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?

like image 787
DmitryA Avatar asked Dec 03 '25 10:12

DmitryA


1 Answers

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)

like image 177
tim_yates Avatar answered Dec 04 '25 23:12

tim_yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!