Submitted by discordantus (Sat Jan 17 05:01:44 UTC 2004)
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
Comments | Current voting | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
RCRchive copyright © David Alan Black, 2003-2005.
Powered by .
Shin'ichiro Hara is working on C implementation of Complex, which I think, will be merged in the future version. This proposal will be accepted. The C version of Complex may or may not be built in, depending on the status of RCR#194.
-matz.
I believe Complex should have a C API. It is likely a programmer will be using C for numerical work. This implies Complex must be built in, I think, because symbols loaded from a shared library are not available transitively. That is, an extension cannot, in general, use the C functions of Complex if it is implemented as an extension. One could use rb_funcall everywhere, but this sort of defeats the original purpose of implementing Complex in C. --Jeff Mitchell