diff options
| author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-24 20:55:40 +0000 |
|---|---|---|
| committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-10-24 20:55:40 +0000 |
| commit | a5d3cd4c5cd43a36e2f2232aa1c5e49136696ec1 (patch) | |
| tree | 39c97af077c709d5ba2fac8cdd36ff3c8fd14d7a | |
| parent | e414a505bb3494df5eb34e19516f1e5eb3fec50f (diff) | |
| download | ruby-a5d3cd4c5cd43a36e2f2232aa1c5e49136696ec1.tar.gz ruby-a5d3cd4c5cd43a36e2f2232aa1c5e49136696ec1.tar.xz ruby-a5d3cd4c5cd43a36e2f2232aa1c5e49136696ec1.zip | |
* lib/matrix.rb (**): Optimization (up to 45% faster)
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@25457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | lib/matrix.rb | 16 |
2 files changed, 9 insertions, 11 deletions
@@ -1,3 +1,7 @@ +Sun Oct 25 05:44:34 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca> + + * lib/matrix.rb (**): Optimization (up to 45% faster) + Sat Oct 24 14:28:40 2009 Yukihiro Matsumoto <matz@ruby-lang.org> * hash.c (rb_hash_set_default_proc): checks arity of defalt_proc diff --git a/lib/matrix.rb b/lib/matrix.rb index b577ef311..3c75751e3 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -642,18 +642,12 @@ class Matrix return Matrix.identity(self.column_size) if other == 0 other = -other end - z = x - n = other - 1 - while n != 0 - while (div, mod = n.divmod(2) - mod == 0) - x = x * x - n = div - end - z *= x - n -= 1 + z = nil + loop do + z = z ? z * x : x if other[0] == 1 + return z if (other >>= 1).zero? + x *= x end - z elsif other.kind_of?(Float) || defined?(Rational) && other.kind_of?(Rational) Matrix.Raise ErrOperationNotDefined, "**" else |
