• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on primitive types and collections

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 16
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Razvan Popovici
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
A wop bop a lu bop a womp bam boom! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic