Redian新闻
>
这段实例化的代码可以优化提速吗?
avatar
这段实例化的代码可以优化提速吗?# Java - 爪哇娇娃
b*i
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;
}
avatar
r*l
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");

avatar
n*8
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");

avatar
a*n
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");

avatar
b*i
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);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。