aspose file tools
The moose likes Ruby and the fly likes Question on primitive types and collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Other Languages » Ruby
Reply Bookmark "Question on primitive types and collections" Watch "Question on primitive types and collections" New topic
Author

Question on primitive types and collections

Razvan Popovici
Greenhorn

Joined: Feb 15, 2007
Posts: 14
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?
David Black
author
Greenhorn

Joined: Aug 16, 2009
Posts: 13
Hi Razvan --

In general, at least in the original interpreter, an array is internally an array of VALUE pointers, where VALUE is the type representing Ruby objects. If you do:

str = "hi"
array = [str]

then array[0] is basically a pointer to the str object.

There are some optimizations involving handling of integers, but I'm not sure how much of a difference that makes in array handling.


David


Ruby training coming up in September! David A. Black and Erik Kastner team up for fast-paced, four-day Ruby intro, in New Jersey, September 14-17. See http://rubyurl.com/vmzN or contact David.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11945
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.


Author of Test Driven (Manning Publications, 2007) [Blog] [HowToAskQuestionsOnJavaRanch]
Razvan Popovici
Greenhorn

Joined: Feb 15, 2007
Posts: 14
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
Sheriff

Joined: Jan 23, 2002
Posts: 11945
Razvan Popovici wrote:
Lasse Koskela wrote:Ah. That was time wasted for a useless benchmark. And fun.

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

Sure. You can find it over here.
 
 
subject: Question on primitive types and collections
 
Threads others viewed
primitive types in java
wrapper classes
parser integer to string
Time to revisit the primitive immutable wrapper objects in the java.lang package?
declaration vs definition
MyEclipse, The Clear Choice