[MacRuby-devel] Macruby 10x slower than built-in Ruby for my script
zhida cheng
vorbei at gmail.com
Wed Jul 1 06:26:49 PDT 2009
Hi all,
I'm using ruby for some simple scripts for data processing,
and I'm interested in Macruby since it's a mac friendly framework and
very snappy too.
But in this very simple (I think) script, Macruby is 10x slower than
built-in Ruby in OSX 10.5.7
It's for reading atom positions, calculating distances, make a summary
and find out nearest atoms.
You can get the codes here: http://gist.github.com/138760
------------------------------------------------------------------------------------------------------------------
require 'scanf'
arr=IO.readlines(ARGV[0])
pos_data = arr[4..-1]
pos = pos_data.collect{|a| a.scanf('%d %d %f %f %f')[2..4]}
dist_matrix = pos.collect { |i| pos.collect{|j| (((i[0]-j[0])**2+(i[1]-
j[1])**2+(i[2]-j[2])**2)**0.5*0.529*100).round/100.0}}
dist_uniq = dist_matrix.inject([]) { |sum,i| sum|i } - [0.0]
dist_min = dist_uniq.min
dist_dup = dist_matrix.inject([]) { |sum,i| sum+i }
dist_sum = dist_dup.inject(Hash.new(0)) { |h,i| h[i] += 1; h}
dist_sum_sort = dist_sum.sort.find_all {|i| i[0]<3.0}
dist_sum_sort.each { |i| puts "%.2f %d" %i }
dist_matrix.each_with_index{|item,i|
a=item.index(dist_min)
if a then
puts "%.2f: %d and %d"%[dist_min, a, i]
end
}
------------------------------------------------------------------------------------------------------------------
$time macruby dist.rb 309.xv
real 0m17.084s
user 0m26.893s
sys 0m1.330s
$time ruby dist.rb 309.xv
real 0m1.052s
user 0m0.981s
sys 0m0.069s
$time macruby-exp dist.rb 309.xv
real 0m9.934s
user 0m17.090s
sys 0m0.643s
-----------------------------------------------------------------------------------------------------------------------
the 309.xv file is too lengthy for email so you can download from the
link.
I also tested on several Macs and It's always the same.
Any ideas? I admit the codes is not well optimized,
but I don't think macruby should be so slow. Even 309**2 is not a too
big number for current machines.
Thanks,
Sincerely
Cheng
More information about the MacRuby-devel
mailing list