写文件 faster and faster
来源:互联网 发布:网易imap服务器和端口 编辑:程序博客网 时间:2024/06/05 11:15
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Test {
public static void main(String[] args) {
int max = 100000000;
long t1 = System.currentTimeMillis();
test1(max);
long t2 = System.currentTimeMillis();
System.out.println("test1耗时:"+(t2-t1));
long t3 = System.currentTimeMillis();
test2(max);
long t4 = System.currentTimeMillis();
System.out.println("test2耗时:"+(t4-t3));
long t5 = System.currentTimeMillis();
test3(max);
long t6 = System.currentTimeMillis();
System.out.println("test3耗时:"+(t6-t5));
//两次测试结果
//已知 max=100000000
//test1耗时:46593/52906
//test2耗时:28563/28250
//test3耗时:21859/22594
}
public static void test1(int max){
BufferedWriter bw = null;
String fileName = "d:/1.txt";
try{
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
bw.write(i+"\r\n");
}
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void test2(int max){
BufferedWriter bw = null;
String fileName = "d:/2.txt";
try{
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
bw.write(String.valueOf(i));
bw.write("\r\n");//快于bw.newLine()
}
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void test3(int max){
BufferedWriter bw = null;
String fileName = "d:/3.txt";
try{
StringBuffer sb = new StringBuffer();
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
sb.append(i);
sb.append("\r\n");
//10000写入一次
if(i%10000==0){
bw.write(sb.toString());
sb.delete(0, sb.length());
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
import java.io.FileWriter;
public class Test {
public static void main(String[] args) {
int max = 100000000;
long t1 = System.currentTimeMillis();
test1(max);
long t2 = System.currentTimeMillis();
System.out.println("test1耗时:"+(t2-t1));
long t3 = System.currentTimeMillis();
test2(max);
long t4 = System.currentTimeMillis();
System.out.println("test2耗时:"+(t4-t3));
long t5 = System.currentTimeMillis();
test3(max);
long t6 = System.currentTimeMillis();
System.out.println("test3耗时:"+(t6-t5));
//两次测试结果
//已知 max=100000000
//test1耗时:46593/52906
//test2耗时:28563/28250
//test3耗时:21859/22594
}
public static void test1(int max){
BufferedWriter bw = null;
String fileName = "d:/1.txt";
try{
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
bw.write(i+"\r\n");
}
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void test2(int max){
BufferedWriter bw = null;
String fileName = "d:/2.txt";
try{
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
bw.write(String.valueOf(i));
bw.write("\r\n");//快于bw.newLine()
}
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void test3(int max){
BufferedWriter bw = null;
String fileName = "d:/3.txt";
try{
StringBuffer sb = new StringBuffer();
bw = new BufferedWriter(new FileWriter(fileName, true));
for(int i=0;i<max;i++){
sb.append(i);
sb.append("\r\n");
//10000写入一次
if(i%10000==0){
bw.write(sb.toString());
sb.delete(0, sb.length());
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
//两次测试结果
//已知 max=100000000
//test1耗时:46593/52906
//test2耗时:28563/28250
//test3耗时:21859/22594
test1采用字符串连接的写入方式,一次写入一行,耗时最长;test2采用分别写入方式,耗时近1半;test3采用批量写入耗时不足一半。由此可知,test3的写入效率最高。 test2和test3比较 每次循环test3 write一次,test2 write两次。
- 写文件 faster and faster
- flex faster and faster
- Faster
- Make Linux faster and lighter
- MCMC and faster Gibbs Sampling using Rcpp
- Fast rcnn and Faster rcnn 算法原理
- Building Faster APIs with NodeJs and Redis
- How To Work Faster And Better
- C51,faster,faster,we need faster
- py-faster-rcnn中的solver文件
- Faster Apple
- Faster rcnn
- Faster-rcnn
- Faster rcnn
- Faster-Rcnn
- faster-rcnn
- faster-RCNN
- Faster-RCNN
- 使用SecureCRT上传和下载文件
- linux socket断开网络连接程序终止
- 嵌入式 Linux 与linux启动时自动加载模块
- MFC内存泄露检测
- Enum的使用
- 写文件 faster and faster
- java的反射机制(简单解释)
- 使用maven构建web项目实例
- wpf调用外部线程操作ui方法
- 【Java工程师之路】[2-3.1]Oracle用户、权限、角色管理
- 2014找工作总结-机会往往留给有准备的人
- 如何在Oracle中复制表结构和表数据、查看有哪些表
- 过QQ游戏大厅的SX保护
- 《Android底层开发技术》学习总结