2

I have an array of 10_000 elements or more, and I have got a bunch of random numbers, for example, say [10,20, 45, 15, 99, 682, 100]. I know that I can loop over array and access or set some value at these indexes. My questions is that can I assign values to these indexes at once without looping to make it CPU efficient.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • No, you can not. – Sergio Tulentsev Oct 24 '17 at 17:58
  • 3
    You don't need to loop over the entire array of 10_000 elements. You just need to loop over `[10,20, 45, 15, 99, 682, 100]`. – sawa Oct 24 '17 at 18:03
  • @sawa in real time I have an array of 1 million records, and thousands of random numbers and at those indexes I have to assign a value say `0`., and by looping i mean to iterate of these given random numbers. – Sachin Singh Oct 24 '17 at 18:05
  • What you're looking for is something like: `foo=[]; foo[3,5,7,8,12,40]=[0,0,0,0,0,0]`. [The documentation of `Array#[]=`](https://ruby-doc.org/core-2.3.1/Array.html#method-i-5B-5D-3D) clearly shows that this isn't possible (as would be, say, in Perl). – Derrell Durrett Oct 24 '17 at 18:23
  • Yes was expecting something like this foo[10,15]=0. – Sachin Singh Oct 24 '17 at 18:26
  • 1
    _Sidenote:_ do not use backticks for emphasis. – Aleksei Matiushkin Oct 24 '17 at 18:32
  • This feels like a data structured problem. At least for the case of access you could represent the data as a hash and use `#values_at`. – hoffm Oct 24 '17 at 18:34
  • 2
    @hoffm: arrays have `values_at` too. Still, be it read or write, one can't escape that loop/enumeration. It's fundamentally impossible. :) – Sergio Tulentsev Oct 24 '17 at 18:35
  • I have to assign values, dont want to get it. – Sachin Singh Oct 24 '17 at 18:36
  • @SergioTulentsev Good point. I retract that specific suggestion. But it still feels like representing the data differently could make this possible, if one cares only about CPU efficiency. – hoffm Oct 24 '17 at 18:36
  • 1
    @hoffm if one cares about CPU efficiency that much, they should not use [tag:ruby] for that in the first place. Native extension in [tag:c] with array created with an explicit `malloc` and direct memory access would do, for instance. – Aleksei Matiushkin Oct 24 '17 at 18:42
  • 1
    @mudasobwa: I disagree. There are plenty of examples of well-performing Ruby code. Rubinius has thrown away their JIT and are working on a new one, but back when they *did* still have one, their `Hash` class (written in pure Ruby) was as performant as YARV's (written in C). And Chris Seaton has demonstrated pure-Ruby image manipulation code on TruffleRuby outperforming the same code using a native C extension on YARV. So, clearly, Ruby *can* be as fast as, and even faster than C, given a good enough compiler. (TruffleRuby has also demonstrated that its C interpreter can run C extensions … – Jörg W Mittag Oct 24 '17 at 21:44
  • … as fast as YARV can run them natively compiled, but that is a different interesting question.) – Jörg W Mittag Oct 24 '17 at 21:45
  • You can always use [Crystal](https://crystal-lang.org/)... – Mark Thomas Oct 24 '17 at 22:17
  • 1
    @JörgWMittag I am not sure I follow what exactly do you disagree with. Ruby due to it’s nature has no ability to directly address memory and there is no way to outperform direct memory access while retrieving values. Single `get` using the implementation I proposed in the previous comment requires exactly 3 assembly instructions (including multiplication by value’s length.) There is no way to outperform this _with an implementation_, no matter how great is it. – Aleksei Matiushkin Oct 25 '17 at 04:48

0 Answers0