在Java数组性能和内存使用VS C ++数组 [英] Performance and memory usage in Java arrays vs C++ arrays

查看:98
本文介绍了在Java数组性能和内存使用VS C ++数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一家小公司,我的工作建立一些银行软件工作。现在,我必须建立像一些数据结构:

 阵列[中间体最大] [2] //大型二维数组

保存了磁盘和第二天加载它为今后的工作。

现在,我只知道Java语言(点点C),他们始终坚持我使用C ++或C按他们的建议是:


  1. 他们看到阵列[中间体最大] [2] Java将需要将近1.5倍以上的内存比C和C ++需要一些什么合理的内存占用比Java。


  2. C和C ++能够处理任意大的文件,其中如Java不能。


根据他们的建议,数据库/数据结构成为大型Java只是变得不可行。正如我们在这样的大的数据库/数据结构工作,C / C ++总是preferable。

现在我的问题是,


  1. 为什么C或C ++总是对Java美元大的数据库/数据结构p $ pferable?因为,C可以是,但C ++中也是一个面向对象的。那么,如何得到它的优势对Java?


  2. 我应该留在Java或他们的建议(切换到C ++)将在未来有所帮助大型数据库/数据结构的环境?任何建议?


对不起,我把所有那些刚开始在一个项目的很少的知识,所以真的很困惑。因为到现在为止我刚刚建立一些学校的项目,没有关于比较大的项目的想法。


解决方案

  

为什么C / C ++总是在美元大的数据库/数据结构p $ pferable
  Java的?因为,C可以是,但C ++中也是一个面向对象的。那么,它是如何获得
  优势的Java?


请记住,Java数组(对象) 1 实际上是引用数组。为简单起见让我们来看看一维数组:

Java的:

  [REF1,REF2,REF3,...,REFN]
REF1 - > object1
给定2 - > Object2的
...
雷弗恩 - > objectN

C ++:

  [object1,Object2的,...,objectN]

使用C ++版本当不需要数组中引用的开销,数组中存放的对象本身 - 而不仅是他们的引用。如果对象是小 - 这种开销可能确实是显著

另外,我在评论中已经说过 - 还有另外一个问题,数组和Java调用C小物体时++。在C ++中,你分配一个对象数组 - 他们连续在内存中,而在Java - 对象本身都没有。在某些情况下,可能会导致C ++有许多更好的性能,因为它是更高速缓存效率则Java程序。有一次,我解决了这个问题,这个线程


  

2)我应该留在Java或他们的建议(切换到C ++)会
  大型数据库/数据结构有帮助的环境在未来?任何
  建议?


我不相信我们能为您一一解答。你应该知道你的目的每个接口的所有优点和缺点(内存效率,图书馆就可以使用,开发时间,...),并作出决定。不要害怕摆脱你公司学长开发谁对系统的详细信息,然后我们都建议。
结果如果有一个简单方便,一般的回答这个问题 - 是没有必要,我们的工程师,是不是我们

您也可以与预期数组的大小和实施的核心之前存根算法分析您的code和简介它来看看有什么真正的区别预计。 (假设数组确实是预期的主要空间使用者)


1 我所描述接下来的开销是不相关的原语的数组。在这些情况下(原语)的阵列的,而不是的值数组的引用的,同C ++,与数组本身较小的开销(长度字段,例如)。

I work on a small company where I work to build some banking software. Now, I have to build some data structure like:

Array [Int-Max] [2] // Large 2D array

Save that to disk and load it next day for future work.

Now, as I only know Java (and little bit C), they always insist me to use C++ or C. As per their suggestion:

  1. They have seen Array [Int-Max] [2] in Java will take nearly 1.5 times more memory than C and C++ takes some what reasonable memory footprint than Java.

  2. C and C++ can handle arbitrarily large files where as Java can't.

As per their suggestion, as database/data-structure become large Java just becomes infeasible. As we have to work on such large database/data-structure, C/C++ is always preferable.

Now my question is,

  1. Why is C or C++ always preferable on large database/data-structure over Java ? Because, C may be, but C++ is also an OOP. So, how it get advantage over Java ?

  2. Should I stay on Java or their suggestion (switch to C++) will be helpful in future on large database/data-structure environment ? Any suggestion ?

Sorry, I have very few knowledge of all those and just started to work on a project, so really confused. Because until now I have just build some school project, have no idea about relatively large project.

解决方案

why C/C++ is always preferable on large database/data-structure over Java ? Because, C may be, but C++ is also an OOP. So, how it get advantage over Java ?

Remember that a java array (of objects)1 is actually an array of references. For simplicity let's look at a 1D array:

java:

[ref1,ref2,ref3,...,refN]
ref1 -> object1
ref2 -> object2
...
refN -> objectN

c++:

[object1,object2,...,objectN]

The overhead of references is not needed in the array when using the C++ version, the array holds the objects themselves - and not only their references. If the objects are small - this overhead might indeed be significant.

Also, as I already stated in comments - there is another issue when allocating small objects in C++ in arrays vs java. In C++, you allocate an array of objects - and they are contiguous in the memory, while in java - the objects themselves aren't. In some cases, it might cause the C++ to have much better performance, because it is much more cache efficient then the java program. I once addressed this issue in this thread

2) Should I stay on Java or their suggestion (switch to C++) will be helpful in future on large database/data-structure environment ? Any suggestion ?

I don't believe we can answer it for you. You should be aware of all pros and cons (memory efficiency, libraries you can use, development time, ...) of each for your purpose and make a decision. Don't be afraid to get advises from seniors developers in your company who have more information about the system then we are.
If there was a simple easy and generic answer to this questions - we engineers were not needed, wouldn't we?

You can also profile your code with the expected array size and a stub algorithm before implementing the core and profile it to see what the real difference is expected to be. (Assuming the array is indeed the expected main space consumer)


1: The overhead I am describing next is not relevant for arrays of primitives. In these cases (primitives) the arrays are arrays of values, and not of references, same as C++, with minor overhead for the array itself (length field, for example).

这篇关于在Java数组性能和内存使用VS C ++数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆