FileUtils.write 写入速度
FileUtils.write writing speed
我正在尝试读取 mysql 并将结果写入 txt 文件。如您所见,我使用 Apache 的 Commons IO。结果集包含推文和每个 sql 以下近 returns 725 行的查询将被写入 txt 文件。我的问题是写入速度,它非常慢(每秒 2-3 kb)。我在这里遗漏了什么吗?
Statement stmt2 = connection.createStatement();
for (int week = 0 ; week<hashTag.length/15 ; week++){
File container = new File("C:\Users\COMP\Desktop\threeMonthsSplitTxt\weeklyBinsTwitter\week"+week+"-"+hashTag[week]+".txt");
for(int hash = 0 ; hash<15 ; hash++){
ResultSet results = stmt2.executeQuery("select tweetContent
from threemonthswithhashtag
where hashTag = '"+hashTag[hashCount]+"'
and tweetCreatedTime between '"+firstDate[hashCount]+"'
and '"+ lastDate[hashCount]+"';");
while(results.next()){
tweetContent = results.getString("tweetContent");
try{
FileUtils.write(container,newLine,"UTF8",true);
FileUtils.write(container,tweetContent,"UTF8",true);
}catch(IOException e){e.getMessage();}
}
hashCount++;
}
}
您正在使用 API,它将 create/open/close 一个文件(句柄)用于 每个 写操作。
您很惊讶这并没有给您带来最佳性能?!
那个实用方法可能很方便,但哎呀,而不是去
loop:
try:
open file; write to file; close file
open file; write to file; close file
考虑按照
的方式做一些事情
open file
loop:
try:
write to open file
write to open file
close file
相反。当然,这意味着您将不得不编写 更多 代码;让事情变得更复杂;但是好吧:有时必须平衡 "super-easy to read" 代码和 "performing good enough" 代码。
可能最多的返工甚至会像这样:
StringBuilder toWrite = ...
loop:
try:
toWrite.append(...)
toWrite.append(...)
然后,在循环之后,您使用 FileUtils.write()
来简单地将全部内容(您在内存中收集的内容)一次性 写入文件系统。
这应该使新代码的整体复杂性保持在合理的水平;但有助于提高 end-to-end 性能。
我正在尝试读取 mysql 并将结果写入 txt 文件。如您所见,我使用 Apache 的 Commons IO。结果集包含推文和每个 sql 以下近 returns 725 行的查询将被写入 txt 文件。我的问题是写入速度,它非常慢(每秒 2-3 kb)。我在这里遗漏了什么吗?
Statement stmt2 = connection.createStatement();
for (int week = 0 ; week<hashTag.length/15 ; week++){
File container = new File("C:\Users\COMP\Desktop\threeMonthsSplitTxt\weeklyBinsTwitter\week"+week+"-"+hashTag[week]+".txt");
for(int hash = 0 ; hash<15 ; hash++){
ResultSet results = stmt2.executeQuery("select tweetContent
from threemonthswithhashtag
where hashTag = '"+hashTag[hashCount]+"'
and tweetCreatedTime between '"+firstDate[hashCount]+"'
and '"+ lastDate[hashCount]+"';");
while(results.next()){
tweetContent = results.getString("tweetContent");
try{
FileUtils.write(container,newLine,"UTF8",true);
FileUtils.write(container,tweetContent,"UTF8",true);
}catch(IOException e){e.getMessage();}
}
hashCount++;
}
}
您正在使用 API,它将 create/open/close 一个文件(句柄)用于 每个 写操作。
您很惊讶这并没有给您带来最佳性能?!
那个实用方法可能很方便,但哎呀,而不是去
loop:
try:
open file; write to file; close file
open file; write to file; close file
考虑按照
的方式做一些事情open file
loop:
try:
write to open file
write to open file
close file
相反。当然,这意味着您将不得不编写 更多 代码;让事情变得更复杂;但是好吧:有时必须平衡 "super-easy to read" 代码和 "performing good enough" 代码。
可能最多的返工甚至会像这样:
StringBuilder toWrite = ...
loop:
try:
toWrite.append(...)
toWrite.append(...)
然后,在循环之后,您使用 FileUtils.write()
来简单地将全部内容(您在内存中收集的内容)一次性 写入文件系统。
这应该使新代码的整体复杂性保持在合理的水平;但有助于提高 end-to-end 性能。