| |
格式化开销
实际上,将数据写入文件只是输出开销的一部分。另外一个巨大的开销是数据的格式 化。考虑下面的三个例 子,要求其输出如下的行:
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);
}
}
}
方法 3
第三种方法使用了java.text包中的类messageformat:
import java.text.*;
public class format3 {
public static void main(string args[]) {
messageformat fmt =
new messageformat("the square of {0} is {1}/n");
object values[] = new object[2];
int n = 5;
values[0] = new integer(n);
values[1] = new integer(n * n);
final int count = 25000;
for (int i = 1; i <= count; i++) {
string s = fmt.format(values);
system.out.print(s);
}
}
}
这些程序产生相同的输出,运行时间为:
format1 1.3
format2 1.8
format3 7.8
最快和最慢之间的差距为6比1。如果该格式没有进行预编译,并且采用了便利的静态方法,第三个程序将更 慢。
方法 4
使用messageformat.format(string, object[])方法如下:
import java.text.*;
public class format4 {
public static void main(string args[]) {
string fmt = "the square of {0} is {1}/n";
object values[] = new object[2];
int n = 5;
values[0] = new integer(n);
values[1] = new integer(n * n);
final int count = 25000;
for (int i = 1; i <= count; i++) {
string s = messageformat.format(fmt, values);
system.out.print(s);
}
}
}
这比前一个例子花费的时间还要长1/3。
方法3比1、2慢一点,并不意味不应该采用它。但是,应该明白在时间上的代价。
在国际化语言环境中,消息的格式是非常重要的,涉及到这个问题的应用程序通常从一个资源文件中读取该 格式,然后使用它。
随机存储
randomaccessfile是用于对文件进行随机i/o存储(在字节层次上)的一个java类。该类提供了一个与c/c++中 相似的搜索方法,以将文件指针移动到任意位置,然后就可以对从那里开始的字节进行读或写了。 该搜索方法访问底层的运行系统,正因为如此,开销可能非常昂贵。一个稍微廉价的替代方法是,在 randomaccessfile顶部设置自己的缓冲,并且实现对字节的直接读取方法。用于读取的参数是所需字节的字节 偏移量。下面的例子显示了这是如何进行的:
import java.io.*;
public class readrandom {
private static final int default_bufsize = 4096;
private randomaccessfile raf;
&
|
|