Performance Groovy I/O性能问题
我不是一个groovy专家,只是偶尔使用它。最新的目标之一是生成一个包含一些随机数据的非常简单的文件。我创建了以下脚本:Performance Groovy I/O性能问题,performance,groovy,Performance,Groovy,我不是一个groovy专家,只是偶尔使用它。最新的目标之一是生成一个包含一些随机数据的非常简单的文件。我创建了以下脚本: 你知道如何提高groovy的处理速度吗?左移位操作符打开文件,跳到末尾,追加文本,然后再次关闭文件 相反,请尝试: Random random = new Random(); // Open the file and append to it. // If you want a new file each time, use withWriter instead of w
你知道如何提高groovy的处理速度吗?左移位操作符打开文件,跳到末尾,追加文本,然后再次关闭文件 相反,请尝试:
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"
}
}
(这也更像Ruby代码正在做的事情)
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
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"
}
}