Editing Topic: RCR195 Project: RCR | RCRchive home

RCR 195: built-in complex math

submitted by discordantus on Sat Jan 17 2004 12:57:44 AM -0800

Status: pending


Abstract

Speeding up operations using complex numbers by implementing the Complex class as a built-in.

Problem

Currently, math using complex numbers is very slow, because the Complex class is implemented in pure Ruby. This makes it difficult to feel good about using Ruby for anything requiring large amounts of complex number crunching.

Proposal

Complex numbers could be implemented in C as a Ruby built-in class.

Analysis

I did some tests, comparing the performance of Ruby's complex mathematics with that of Python's. Here are the scripts I used:

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

Implementation

The implementation would be in C, presumably including all the features of the current Complex class.


Back to RCRchive.


RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog