1.1.3.2.4 格式化的代价
实际上向文件写数据只是输出代价的一部分。另一个可观的代价是数据格式化。考虑一个三部分程序,它像下面这样输出一行:
The square of 5 is 25
方法 1
第一种方法简单的输出一个固定的字符串,了解固有的I/O开销:
public class format1 {
public static void main(String args[]) {
final int COUNT = 25000;
for (int i = 1; i <= COUNT; i++) {
String s = "The square of 5 is 25\n";
System.out.print(s);
}
}
}
方法2
第二种方法使用简单格式"+":
public class format2 {
public static void main(String args[]) {
int n = 5;
final int COUNT = 25000;
for (int i = 1; i <= COUNT; i++) {
String s = "The square of " + n + " is " + n * n + "\n";
System.out.print(s);
}
}
}