ruby-changes:13672
From: marcandre <ko1@a...>
Date: Sun, 25 Oct 2009 05:55:56 +0900 (JST)
Subject: [ruby-changes:13672] Ruby:r25457 (trunk): * lib/matrix.rb (**): Optimization (up to 45% faster)
marcandre 2009-10-25 05:55:40 +0900 (Sun, 25 Oct 2009) New Revision: 25457 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25457 Log: * lib/matrix.rb (**): Optimization (up to 45% faster) Modified files: trunk/ChangeLog trunk/lib/matrix.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 25456) +++ ChangeLog (revision 25457) @@ -1,3 +1,7 @@ +Sun Oct 25 05:44:34 2009 Marc-Andre Lafortune <ruby-core@m...> + + * lib/matrix.rb (**): Optimization (up to 45% faster) + Sat Oct 24 14:28:40 2009 Yukihiro Matsumoto <matz@r...> * hash.c (rb_hash_set_default_proc): checks arity of defalt_proc Index: lib/matrix.rb =================================================================== --- lib/matrix.rb (revision 25456) +++ lib/matrix.rb (revision 25457) @@ -642,18 +642,12 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/