Question on primitive types and collections

One of my projects involving gene sequence alignment uses a large amount of big linear vectors of integers and floats. I have noticed in Java that if I am using a language provided vector (such as a[] ) the speed improves significantly as when using the implemented collections classes (List, Set, Vector). Also, by using primitive types (such as int, long) the speed improves up to 10 times as when using language classes (Integer, Long, Float). The reason is simple, each Integer is an instance of a class, residing in its own dynamically allocated memory area.
Regarding Ruby, how does Ruby manage the primitive types? I understand that the programmer sees them as classes, but internally, how much memory areas are allocated for example if one builds a vector of 1000 integers? Is this one, as in int[1000] in Java, or 1001 as of Integer[1000]: the array object and the 1000 Integer objects?

Out of curiosity, I wrote a little micro-benchmark by parsing output from 'top' and determined that for an array of million "primitive" integers (well, in Ruby everything is an object) my benchmark seemed to consume 4 bytes per integer, which suggests that the interpreter does indeed have some optimizations for integers.

Lasse Koskela wrote:Out of curiosity, I wrote a little micro-benchmark by parsing output from 'top' and determined that for an array of million "primitive" integers (well, in Ruby everything is an object) my benchmark seemed to consume 4 bytes per integer, which suggests that the interpreter does indeed have some optimizations for integers.

Ah. That was time wasted for a useless benchmark. And fun.

I would like to learn Ruby, can you publish your code?

Lasse Koskela

author

Posts: 11962

5

posted 10 years ago

Razvan Popovici wrote:

Lasse Koskela wrote:Ah. That was time wasted for a useless benchmark. And fun.