运行相同的Java代码Windows vs Unix时的性能差异

时间:2012-05-04 14:35:47

标签: java windows eclipse unix

我通过windows中的eclipse运行代码,然后在Unix中作为独立的java运行。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.SQLException;
import java.text.ParseException;

public class readObjectBoeing {



/**
 * @param args
 * @throws ParseException 
 * @throws SQLException 
 */
public static void main(String[] args) {
    //File file = new File("/opt/app/d1ebp1m1/dv01/Vibhor/test/00017741_repository.dat");
    File file = new File("C:/_Vibhor/00017741_repository.dat");
    InputStream is;
    try {
        is = new FileInputStream(file);
        byte[] b = toByteArray(is);//read from file;
        Object o1 =null;
        o1 = convertByteArrayToObject(b);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public static Object convertByteArrayToObject(byte[] buf) throws Exception 
{

    if (buf.length == 0)
    {
        return null;
    }
    long startTime = -1;
    long step1=-1,step2=-1;
    Object                  obj = null;
    ByteArrayInputStream    bis = null;
    ObjectInputStream       in  = null;
    try 
    {   
        bis = new ByteArrayInputStream(buf);
        in  = new ObjectInputStream(bis);
        startTime = System.currentTimeMillis()/1000;
        obj = in.readObject();
        step1 = System.currentTimeMillis()/1000 - startTime ;
        System.out.println("in.readObject()!! :  " + step1);
    } 
    catch (Exception e) 
    {
        throw e;
    } 
    finally 
    {
        if (in != null) 
        {
            in.close();
        }
        if (bis != null) 
        {
            bis.close();
        }
        in  = null;
        bis = null;
    }

    return obj;
}
public static byte[] toByteArray(InputStream input) throws IOException
{
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    long count = 0L;
    byte[] buffer =new byte[4096];
    for(int n = 0; -1 != (n = input.read(buffer));){
        output.write(buffer, 0, n);
        count += n;
    }
    return output.toByteArray();
}

}

00017741_repository.dat - 它是57Mb文件 在Windows obj = in.readObject();中 - 我需要4-5秒 但是在Unix obj = in.readObject();中我需要19到25秒!

我在两种情况下都使用VM args -Xmx512m执行。

在unix中:

java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Server VM (build 20.4-b02, mixed mode)

在Windows中:

jre 1.6.0_26

我在这里缺少什么?有任何改善unix性能的建议吗?

3 个答案:

答案 0 :(得分:0)

在平台之间找到这样的差异并非令人震惊。理解虽然字节码是相同的,但JVM是特定于平台的。

有很多这样的领域,你可以在一个平台上完美地运行代码而在另一个平台上运行失败。我遇到的一个案例是使用文件拖放,其中unix(至少是ubuntu)删除的文件与windows不同。

您必须在所有要在

中运行代码的平台上进行测试

此外,在您的特定情况下,您应该尝试更有效地读取输入流(使用byte [])或创建BufferedInputStream以包装您的FileInputStream

答案 1 :(得分:0)

以下是您可以做的两件具体事情:

  1. 确保在Unix上,该文件驻留在本地文件系统上而不是网络安装上;
  2. 使用分析器(例如YourKit)找出花费的时间。

答案 2 :(得分:0)

您还必须区分使用的不同JVM。例如在* nix系统上有广泛使用的“icedtea”或“OpenJDK”等虚拟机(因为它们是默认安装)。