62,614
社区成员
发帖
与我相关
我的任务
分享
//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);
}
}
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);
}
}
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);
}
}
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();