高效打印1000000次abc

manyfaces 2010-04-21 09:58:54

//BufferedWriter输出
public static void print(String s, int times) {

BufferedWriter writer = null;
try {
writer = new BufferedWriter(new PrintWriter(System.out));
int len = s.length();
for (int i = 0; i < times; i++) {
writer.write(s, 0, len);
}
writer.flush();

} catch (IOException e) {

} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
//
}

}
}

//常规输出
public static void normalPrint(String s, int times) {
for (int i = 0; i < times; i++) {
System.out.print(s);
}
}



让我很惊讶的junit测试结果

package test;

import junit.framework.TestCase;

public class PrintTest extends TestCase {
private static final String CONTENT = "abc";
private static final int TIMES = 1000000;

public void testPrint() {
problem.LargerNumberOfString.print(CONTENT, TIMES);
}

public void testNormalPrint() {
problem.LargerNumberOfString.normalPrint(CONTENT,TIMES);
}

public static void main(String[] args) {
junit.swingui.TestRunner.run(PrintTest.class);

}
}

结果BufferedWriter输出大致是6S,而普通输出3s
我很费解,不是BufferedWriter会快些么
...全文
211 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyang0180 2010-04-21
  • 打赏
  • 举报
回复
调用层次多了
ArayChou 2010-04-21
  • 打赏
  • 举报
回复
System.out,带有线程同步功能,要优化它,可以减少嗲用 write或者print的次数。

StringBuilder sb= new StringBuilder(5000);
for()
{
sb.apend(xxx);
}
System.out.println(sb);

这样会快不少
groovy2007 2010-04-21
  • 打赏
  • 举报
回复
另外,PrintWriter用的是print方法,而BufferedWriter用的是write方法。
建议都改成使用write方法,再比较一下
groovy2007 2010-04-21
  • 打赏
  • 举报
回复
PrintWriter本身就是带缓冲的,所以没有必要外面再套一个BufferedWriter。
套了之后,多一层调用,所以会变慢。
manyfaces 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 nihaozhangchao 的回复:]

你这样的比较是不行的。。根本就没有一个线上。

Java code

package com.samba.xlh;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintTest……
[/Quote]
比较为什么不行呢,什么叫不再一个线,阁下可否明说,我是用JUnit测试的。
manyfaces 2010-04-21
  • 打赏
  • 举报
回复
楼上的更有意思了,BufferedWriter本来就是用来提速的
crimy10 2010-04-21
  • 打赏
  • 举报
回复
现在只是因为它支持多线程所以还在使用它
crimy10 2010-04-21
  • 打赏
  • 举报
回复
Bufferxxx因为有缓冲区,所以它慢的- -
manyfaces 2010-04-21
  • 打赏
  • 举报
回复
我也这么想,可是结果不是我那么想的,BufferedWriter费了两倍时间,谁能告诉我是我代码不好还是别的什么原因
gloomyfish 2010-04-21
  • 打赏
  • 举报
回复
bufferedReader 和 BufferedWriter有缓冲区
读写大的数据,都推荐用这个!
SambaGao 2010-04-21
  • 打赏
  • 举报
回复
你这样的比较是不行的。。根本就没有一个线上。


package com.samba.xlh;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintTest {

// BufferedWriter输出
public static void print(String s, int times) {

BufferedWriter writer = null;
try {
writer = new BufferedWriter(new PrintWriter(System.out));
int len = s.length();
for (int i = 0; i < times; i++) {
//writer.write(s, 0, len);

}
writer.flush();

} catch (IOException e) {

} finally {

if (writer != null) {
// writer.close();
}
}
}

// 常规输出
public static void normalPrint(String s, int times) {
for (int i = 0; i < times; i++) {
// System.out.print(s);
}
}

private static final String CONTENT = "abc";
private static final int TIMES = 100000000;

public static void main(String args[]) {

long start = System.currentTimeMillis();
// normalPrint(CONTENT,TIMES) ; //125 141 141 93
print(CONTENT, TIMES);
long end = System.currentTimeMillis();
System.out.println(end - start);
}

}



平常就比BufferedWrite 要快可是当
http://xyiyy.javaeye.com/blog/361476
keeya0416 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 keeya0416 的回复:]
试试这个,这个是输出最快的
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
[/Quote]
我做ACM就是用这个输出的 比System.out.println快很多
keeya0416 2010-04-21
  • 打赏
  • 举报
回复
试试这个,这个是输出最快的
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
whlcy 2010-04-21
  • 打赏
  • 举报
回复
值得考虑!!!
Dan1980 2010-04-21
  • 打赏
  • 举报
回复
首先,System.out是一个OutputStream,直接处理字节,比处理字符的Writer要快。
其次,System.out本身就带有缓冲,只不过,它是一个自动flush的缓冲,就是遇到换行时flush。所以,你用System.out.print(),其实它还是在缓冲里面的,把print换成println,你再测试。
manyfaces 2010-04-21
  • 打赏
  • 举报
回复
我查看了一下PrinterWriter,确实装配了一个BufferedWriter
BufferedWriter的工作是先存贮数据到缓冲区,默认大小有8000多字节,等满了就刷新缓冲区,调用writer的write,但是我把代码改成
PrintWriter printer = new PrintWriter(System.out);
int len = s.length();
for (int i = 0; i < times; i++) {
printer.write(s, 0, len);
}
printer.flush();
printer.close();

之后的test结果
普通打印9.8S
PrinterWriter需要19.312s之多
manyfaces 2010-04-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 araychou 的回复:]

System.out,带有线程同步功能,要优化它,可以减少嗲用 write或者print的次数。

StringBuilder sb= new StringBuilder(5000);
for()
{
sb.apend(xxx);
}
System.out.println(sb);

这样会快不少
[/Quote]
这样不是相当于自己做个BufferedWriter么?兄台

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧