[Mono-dev] Int32.CompareTo() enhancement

Alan McGovern alan.mcgovern at gmail.com
Thu Nov 22 16:05:56 EST 2007


I was just looking at the source of int32, and i noticed that there was room
for improvement in the implementation of CompareTo.

This implementation is approx 33% faster than the existing one. Is this ok
to commit? I can write the changelog and commit myself if i get the
go-ahead:

Index: Int32.cs
===================================================================
--- Int32.cs    (revision 90185)
+++ Int32.cs    (working copy)
@@ -57,12 +57,7 @@
                                throw new ArgumentException
(Locale.GetText("Value is not a
System.Int32"));

                        int xv = (int) v;
-                       if (m_value == xv)
-                               return 0;
-                       if (m_value > xv)
-                               return 1;
-                       else
-                               return -1;
+                       return m_value - v;
                }

                public override bool Equals (object o)
@@ -81,12 +76,7 @@
 #if NET_2_0
                public int CompareTo (int value)
                {
-                       if (m_value == value)
-                               return 0;
-                       if (m_value > value)
-                               return 1;
-                       else
-                               return -1;
+                       return m_value - value;
                }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20071122/d9170c12/attachment.html 


More information about the Mono-devel-list mailing list