繁体   English   中英

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

[英]Performance difference when running same Java code Windows vs 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的性能吗?

在平台之间发现这样的差异并不令人震惊。 要知道,虽然字节码相同,但JVM是平台特定的。

有很多类似的领域,在这些领域中,您的代码可以在一个平台上完美运行,而在另一个平台上则失败。 我遇到的一种情况是文件拖拽,unix(至少是ubuntu)拖拽数据风格与Windows不同的文件。

您必须在要运行代码的所有平台上进行测试

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

您可以执行以下两项具体操作来解决这一问题:

  1. 确保在Unix上,文件位于本地文件系统上,而不位于网络安装上;
  2. 使用探查器(例如YourKit )来找出时间在哪里。

您还必须在所使用的不同JVM之间有所不同。 例如,在* nix系统上,有诸如icedtea或OpenJDK之类的vm被广泛使用(因为它们是默认安装)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM