Ruby:
require 'complex'
(1..15).each do |x|
(1..15).each do |y|
(1..15).each do |u|
(1..15).each do |v|
a = Complex.new(x,y)
b = Complex.new(u,v)
[a*b,a/b,a-b,a+b]
end
end
end
end
Python:
for x in range(1,16):
for y in range(1,16):
for u in range(1,16):
for v in range(1,16):
a = x+y*1j
b = u+v*1j
[a*b,a/b,a-b,a+b]
The Ruby version took 39.04s of processor time to complete the script. The Python version took 2.24s of processor time.
I tried it again, but with the mathematical operations excised and just the complex number creation and assignment intact. This was just to see how much of the speed difference was tied to the fact that I was creating over 50000 complex numbers. With those modifications, the Ruby version took 4.38s while Python took 1.22s.
These tests suggest that if a built-in version of Ruby's complex math is even half as fast as Python's, it would give nearly a 90% reduction in computation time
-matz.
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog