b***i 发帖数: 3043 | 1 某个类的文件本地读入和处理需要0.5秒,我就想能不能串行化,然后我直接读入。结
果发现,直接本地读入object需要1秒。下面就是代码,请问有没有办法加速?obj文件
一共几百k bytes.
public static Object load(String filename){
try {
FileInputStream fin = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(fin);
Object obj = ois.readObject();
ois.close();
System.out.println("unserialized theQueue");
return obj;
} catch (Exception e) { e.printStackTrace(); }
return null;
} |
r*****l 发帖数: 2859 | 2 Seiralization/Deserialization may be expensive. Depending on your specific
need, you can find multiple possible solutions:
For example, you can store just the state of the object and construct that
object after you read the state.
You can also use multiple threads to read in parallel if throughput, not
response time, is important.
You can also spend money to buy high-speed storage to reduce I/O.
...
【在 b***i 的大作中提到】 : 某个类的文件本地读入和处理需要0.5秒,我就想能不能串行化,然后我直接读入。结 : 果发现,直接本地读入object需要1秒。下面就是代码,请问有没有办法加速?obj文件 : 一共几百k bytes. : public static Object load(String filename){ : try { : FileInputStream fin = new FileInputStream(filename); : ObjectInputStream ois = new ObjectInputStream(fin); : Object obj = ois.readObject(); : ois.close(); : System.out.println("unserialized theQueue");
|
n******8 发帖数: 172 | 3
try other approach to read the file, might help.
【在 b***i 的大作中提到】 : 某个类的文件本地读入和处理需要0.5秒,我就想能不能串行化,然后我直接读入。结 : 果发现,直接本地读入object需要1秒。下面就是代码,请问有没有办法加速?obj文件 : 一共几百k bytes. : public static Object load(String filename){ : try { : FileInputStream fin = new FileInputStream(filename); : ObjectInputStream ois = new ObjectInputStream(fin); : Object obj = ois.readObject(); : ois.close(); : System.out.println("unserialized theQueue");
|
a*******n 发帖数: 237 | 4 try to read the file into memory first.
File file = new File(filename);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[file.length()];
fis.read(buffer);
fis.close();
ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
ObjectInputStream ois = new ObjectInputStream(bais);
【在 b***i 的大作中提到】 : 某个类的文件本地读入和处理需要0.5秒,我就想能不能串行化,然后我直接读入。结 : 果发现,直接本地读入object需要1秒。下面就是代码,请问有没有办法加速?obj文件 : 一共几百k bytes. : public static Object load(String filename){ : try { : FileInputStream fin = new FileInputStream(filename); : ObjectInputStream ois = new ObjectInputStream(fin); : Object obj = ois.readObject(); : ois.close(); : System.out.println("unserialized theQueue");
|
b***i 发帖数: 3043 | 5 行,我试试先。
【在 a*******n 的大作中提到】 : try to read the file into memory first. : File file = new File(filename); : FileInputStream fis = new FileInputStream(file); : byte[] buffer = new byte[file.length()]; : fis.read(buffer); : fis.close(); : ByteArrayInputStream bais = new ByteArrayInputStream(buffer); : ObjectInputStream ois = new ObjectInputStream(bais);
|