应学生【弓先生】需求,做此测试,得到帮助的同志心里要谢谢【弓先生】。
代码语言:javascript复制//第一种
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//第二种
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
//第三种
PrintWriter pw = new PrintWriter(System.out);
三种方式时间对比
第一次对比:count=100
第一种:445200纳秒 第二种:4784501纳秒 第三种:411300纳秒
第二次对比:count=10000
第一种:累计10000的值用时:8981600纳秒 第二种:累计10000的值用时:19499900纳秒 第三种:累计10000的值用时:10850900纳秒
两次对比结果
第一种: PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); 效率更高一些,虽然在100次的时候稍显落后于第三种,但是在万级运算中肯明显效率高了。
第三次:十万对比
第一种:累计100000的值用时:4897403801纳秒 第二种:累计100000的值用时:5024694899纳秒 第三种:累计100000的值用时:5069473199纳秒
1、PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
代码语言:javascript复制package test;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class demo {
public static void main(String[] args) {
int count=100;
long start = System.nanoTime();
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
for (int i = 0; i < count; i ) {
pw.println(i);
}
long end = System.nanoTime();
pw.println("累计" count "的值用时:" (end-start) "纳秒");
pw.close();
}
}
2、PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
代码语言:javascript复制package test;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class demo {
public static void main(String[] args) {
int count = 100;
long start = System.nanoTime();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
for (int i = 0; i < count; i ) {
pw.println(i);
}
long end = System.nanoTime();
pw.println("累计" count "的值用时:" (end - start) "纳秒");
pw.close();
}
}
3、PrintWriter pw = new PrintWriter(System.out);
代码语言:javascript复制package test;
import java.io.PrintWriter;
public class demo {
public static void main(String[] args) {
int count = 100;
long start = System.nanoTime();
PrintWriter pw = new PrintWriter(System.out);
for (int i = 0; i < count; i ) {
pw.println(i);
}
long end = System.nanoTime();
pw.println("累计" count "的值用时:" (end - start) "纳秒");
pw.close();
}
}