Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <code>narray</code> for efficient handling of large numeric arrays in Ruby. </p> <p>It is faster, and uses less memory. <a href="http://narray.rubyforge.org/" rel="nofollow">http://narray.rubyforge.org/</a></p> <p>As for the DB storage part. It is possible to add marshalling to <code>narray</code> (which doesn't have it by default):</p> <pre><code># This adds support for Marshal to NArray - found it at: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/194510 class NArray def _dump *ignored Marshal.dump :typecode =&gt; typecode, :shape =&gt; shape, :data =&gt; to_s end def self._load buf h = Marshal.load buf typecode = h[:typecode] shape = h[:shape] data = h[:data] to_na data, typecode, *shape end end </code></pre> <p>. . . which will be quite efficient without you needing to write specific code. </p> <p>If the numbers are completely random, there will be limits to what you can achieve with compression. Truly random data is pretty much incompressible.</p> <p>If you have a good constraining model for how the numbers are arranged, then you can use knowledge of that to create a better compression ratio. The post suggesting to use a <code>Range</code> is an extreme example of that, but of course only works if you have a very simple structure.</p> <p>If the structure is more or less arbitrary, just marshall the data and apply an of-the-shelf compression algorithm to it, like <code>zlib</code>.</p> <p>Edit: The fact that the items are sorted lends itself to better compression. It also ticks a box for <code>narray</code> - it will sort numbers quicker than anything you can do manually with a Ruby <code>Array</code></p> <p>Sample code:</p> <pre><code>require 'narray' require 'zlib' # Ten thousand integers . . . n = NArray.int(10000).random(100000).sort # Compressed . . . stored = Zlib::Deflate.deflate( Marshal.dump(n), 9 ) stored.length =&gt; 14297 </code></pre> <p>That's not bad, 1.5 bytes per number (actual compression ratios will vary a lot, but you'll generally see less than 4 bytes per number using this technique)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload