Mruby faster than ruby 2.0 and jruby?

hello,
trying this stupid code,

def a(p,j)
i=0
while i<1000
p+=j*(j-1)(j-2)(j-3)(j-4)(j-5)(j-6)(j-7)(j-8)(j-9)
i+=1
end
p
end
def b(b)
starttime=Time.now if b
puts(“start”) if b
p=0
j=10
while j<10000
p=a(p,j)
j+=1
end
endtime=Time.now if b
puts(“end : p=”+p.to_s+ " “+ (endtime-starttime).to_f.to_s + " secs”)
if b
end
b(false)
b(true)

===============================
I get this strange result :

jruby j.rb
start
end : p=9041028926052438935146265250228470208000000000 14.0 secs

ruby j.rb
start
end : p=9041028926052438935146265250228470208000000000 26.663525 secs

mruby j.rb
start
end : p=9.04102892605544e+45 2.34413399999999 secs

jruby -v && ruby -v && mruby -v
jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java
HotSpot™ Client VM 1.6.0_31) [Windows 7-x86-java]
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]
mruby 1.0.0 (2014-01-10)

So mruby is it realy the faster ruby vm ?

Hi,

So mruby is it realy the faster ruby vm ?

In general, no. This case is special, since mruby does not have
Bignums and uses faster (and inaccurate) Floats instead.

          matz.

In message “Re: mruby fastr than ruby 2.0 and jruby ?”
on Tue, 18 Feb 2014 18:23:42 +0100, Regis d’Aubarede
[email protected] writes:
|
|hello,
|trying this stupid code,
|=========================
|def a(p,j)
| i=0
| while i<1000
| p+=j*(j-1)(j-2)(j-3)(j-4)(j-5)(j-6)(j-7)(j-8)(j-9)
| i+=1
| end
| p
|end
|def b(b)
| starttime=Time.now if b
| puts(“start”) if b
| p=0
| j=10
| while j<10000
| p=a(p,j)
| j+=1
| end
| endtime=Time.now if b
| puts(“end : p=”+p.to_s+ " “+ (endtime-starttime).to_f.to_s + " secs”)
|if b
|end
|b(false)
|b(true)
|
|===============================
|I get this strange result :
|
|>jruby j.rb
|start
|end : p=9041028926052438935146265250228470208000000000 14.0 secs
|
|>ruby j.rb
|start
|end : p=9041028926052438935146265250228470208000000000 26.663525 secs
|
|>mruby j.rb
|start
|end : p=9.04102892605544e+45 2.34413399999999 secs
|
|>jruby -v && ruby -v && mruby -v
|jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java
|HotSpot™ Client VM 1.6.0_31) [Windows 7-x86-java]
|ruby 2.0.0p0 (2013-02-24) [i386-mingw32]
|mruby 1.0.0 (2014-01-10)
|
|
|
|
|So mruby is it realy the faster ruby vm ?
|
|–
|Posted via http://www.ruby-forum.com/.

So mruby is it realy the faster ruby vm ?

In general, no. This case is special, since mruby does not have
Bignums and uses faster (and inaccurate) Floats instead.

Thank you for your response.

So I replace
p+=j*(j-1)(j-2)(j-3)(j-4)(j-5)(j-6)(j-7)(j-8)(j-9)
by
p+=(1.0j(j-1))/(1.0jj+1)

and
p+=a(p,j)
by
p=a(1,j)

Then I get :

mruby j.rb && ruby j.rb && jruby j.rb
start
end : p=1000.89997999798 1.52808799999999 secs
start
end : p=1000.8999799979861 5.132293 secs
start
end : p=1000.89997999799 3.494 secs

Perhaps windows version give some strange degradation…

Another test with a sudoku resolver :

mrbc sudoku.rb && ruby sudoku.rb && jruby sudoku.rb && mruby -b sudoku.mrb

duration 6.37931 secs, 0.637931 ms by resolution
duration 13.161 secs, 1.3161 ms by resolution
duration 27.4534860 secs, 2.74534860 ms by resolution

code : https://gist.github.com/raubarede/9120207

====
adaptations for mruby:

  • int/int ==> float in mruby , so convert (a/b) by (a/b).floor
  • here docs / DATA not implemented in mruby
  • min_by not exist

original sudoku test is modified for doing backtracking