Hi,
I had feeling that ruby arrays are very slow after I’d run some simple
float operations on array containing only integers.
But after I’ve writen some tests it turns out that the problem is
probably in conversion int to float or float to int.
Strange behaviour 1)
3_000_000 times simple int to float conversion is more then 10 times
slower than float to int
float to int…0.404731428s
int to float…4.803439434s
30_000_000 times int to float is 100 times slower than float to int
float to int…4.057228206
int to float…405.354131022
so 10 times more operations but it’s 100 times slower!
Strange behaviour 2)
with operation multiply it flips:
operation (i * 0.3452523523).to_i is 5 times slower than i *
0.3452523523
(i * 0.3452523523).to_i … 5.187890146
i * 0.3452523523 … 1.054155473
and with 30_000_000 cycles it’s 40 times slower
(i * 0.3452523523).to_i … 413.333509812
i * 0.3452523523 … 9.762297816
(i is integer)
so there the float result conversion to int is now as slow as int to
float in case 1.
I’m confused about this. Could you someone explain me how the
conversion works in ruby and how it is possible?
The test script is attached.
Thanks!
On Aug 17, 2011, at 13:55 , Petr Kovar wrote:
3_000_000 times simple int to float conversion is more then 10 times
slower than float to int
float to int…0.404731428s
int to float…4.803439434s
30_000_000 times int to float is 100 times slower than float to int
float to int…4.057228206
int to float…405.354131022
so 10 times more operations but it’s 100 times slower!
small ints are stored in the VALUE pointer so there is no extra
allocation necessary.
floats aren’t, so every one you make will be a malloc and set and then
that space will be GC’d eventually.
You’re prolly seeing the increase in multiplier by invoking the GC more.
Run on a system with a lot of memory and turn off GC to verify.
small ints are stored in the VALUE pointer so there is no extra allocation
necessary.
floats aren’t, so every one you make will be a malloc and set and then that
space will be GC’d eventually.
You’re prolly seeing the increase in multiplier by invoking the GC more. Run on
a system with a lot of memory and turn off GC to verify.
True! When I disabled GC 30_000_000 cycles are on 8s instead of 400s.
Good to know that if I want to do such kind of operation in large
amount it is good to switch off the GC.
Just for comparing, i’ve done some research with other languages with
default GC settings and the (i * 0.3).to_i operation
ruby 1.9.2 400s
ruby 1.9.3 150s
python3 33s
perl 25s
lua 12s
jruby1.6.2 5s
and c with -O3 0.08s 
2011/8/18 pejuko [email protected]:
and c with -O3 0.08s 
Hey, just so you know, with -O3 the entire multiplying part was
probably optimized away - the compiler computed it and simply stuck a
raw number in the bytecode 
– Matma R.
On Aug 18, 12:09am, Bartosz D. [email protected] wrote:
2011/8/18 pejuko [email protected]:
and c with -O3 0.08s 
Hey, just so you know, with -O3 the entire multiplying part was
probably optimized away - the compiler computed it and simply stuck a
raw number in the bytecode 
– Matma R.

actualy in c it process array with random numbers it include memory
allocation and array initialization and fetching and saving the result
back to the memory.
btw. without any -O the time is 0.3