[Mono-dev] Patch for Math.cs

Cetin Sert cetin.sert at gmail.com
Sun Mar 4 19:47:44 EST 2007


Hi I just checked the IL codes of these methods:

 

private static bool TestIf(int value)

{

    if (value > 0)

        return true;

    else

        return false;

}

 

private static bool TestTurnary(int value)

{

    return value > 0 ? true : false;

}

 

and got these from Lutz Roeder’s famous .NET Reflector (5.0.5.0):

 


.method private hidebysig static
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.
0.0:b77a5c561934e089/System.Boolean> bool
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://ILComparison:
1.0.0.0/ILComparison.Program/TestIf(Int32):Boolean> TestIf(
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.
0.0:b77a5c561934e089/System.Int32> int32 'value') cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: ldc.i4.0 
    L_0002: ble.s L_0006
    L_0004: ldc.i4.1 
    L_0005: ret 
    L_0006: ldc.i4.0 
    L_0007: ret 
}

 

	

.method private hidebysig static
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.
0.0:b77a5c561934e089/System.Boolean> bool
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://ILComparison:
1.0.0.0/ILComparison.Program/TestTurnary(Int32):Boolean> TestTurnary(
<http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib:2.0.
0.0:b77a5c561934e089/System.Int32> int32 'value') cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: ldc.i4.0 
    L_0002: bgt.s L_0006
    L_0004: ldc.i4.0 
    L_0005: ret 
    L_0006: ldc.i4.1 
    L_0007: ret 
}

 

	

As you can see, there is nothing that might cause a performance difference.
It’s only the order of things that’s different.

 

From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Alan McGovern
Sent: Montag, 5. März 2007 01:03
To: Dennis Hayes
Cc: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Patch for Math.cs

 

Just out of interest, could you check the performance of the method if you
change code patterns like:

if (value > 0)
     return Floor (value + 0.5);
else
     return Ceiling (value - 0.5);

to:

return (value > 0) ? Floor(value+0.5) : Ceiling(value - 0.5);

When i was profiling stuff before i think there was a performance boost when
using the turnary form, I may be wrong though. If it does turn out to be
faster, you could make that change to. If it isn't, no worries. 

Alan.



On 3/4/07, Dennis Hayes <denisraytek at yahoo.com> wrote:

This is a minor clean up of Math.cs.

Basicly, in the methods that use MidpointRounding Mode, it moves the check
for a valid mode to the end of the method.

 

Since the mode is checked in the method, doing this eliminates the extra
check. Also, the check should never fail, since an enum is being passed in.
This will make the code a little faster. 

 

I don't have svn access setup, so I need someone to check this in also.

 

I did not include any tests, because this should not have added or modifyed
anything testable. 

 

Should I do anything elseor do anything differently?

Would this be better as a switch?

Thanks,

Dennis

  _____  

Need a quick answer? Get one in minutes from people who know. Ask your
question on Yahoo! Answers
<http://answers.yahoo.com/;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1
NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx> .
--0-164504892-1173052624=:24413--


Index: Math.cs
===================================================================
--- Math.cs     (revision 73696)
+++ Math.cs     (working copy)
@@ -373,27 +373,27 @@ 
               [MonoTODO ("Not implemented")]
               public static decimal Round (decimal d, MidpointRounding
mode)
               {
-                       if ((mode != MidpointRounding.ToEven ) && (mode !=
MidpointRounding.AwayFromZero))
-                               throw new ArgumentException ("The value '" +
mode + "' is not valid for this usage of the type MidpointRounding.",
"mode"); 
-
                       if (mode == MidpointRounding.ToEven)
                               return Round (d);
-                       throw new NotImplementedException ();
+                       if (mode == MidpointRounding.AwayFromZero)
+                           throw new NotImplementedException ();
+
+                       throw new ArgumentException ("The value '" + mode +
"' is not valid for this usage of the type MidpointRounding.", "mode"); 
               }

               [MonoTODO ("Not implemented")]
               public static decimal Round (decimal d, int decimals,
MidpointRounding mode)
               {
-                       if ((mode != MidpointRounding.ToEven) && (mode !=
MidpointRounding.AwayFromZero))
-                               throw new ArgumentException ("The value '" +
mode + "' is not valid for this usage of the type MidpointRounding.",
"mode"); 
-
                       if (mode == MidpointRounding.ToEven)
                               return Round (d, decimals);
-                       throw new NotImplementedException ();
+                       if (mode == MidpointRounding.AwayFromZero)
+                           throw new NotImplementedException ();
+
+                       throw new ArgumentException ("The value '" + mode +
"' is not valid for this usage of the type MidpointRounding.", "mode"); 
               }
 #endif

-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+        [MethodImplAttribute (MethodImplOptions.InternalCall)]
               public extern static double Round (double d); 

               public static double Round (double value, int digits)
@@ -411,26 +411,25 @@
 #if NET_2_0
               public static double Round (double value, MidpointRounding
mode)
               { 
-                       if ((mode != MidpointRounding.ToEven) && (mode !=
MidpointRounding.AwayFromZero))
-                               throw new ArgumentException ("The value '" +
mode + "' is not valid for this usage of the type MidpointRounding.",
"mode"); 
-
                       if (mode == MidpointRounding.ToEven)
                               return Round (value);
                       if (value > 0)
                               return Floor (value + 0.5);
                       else
                               return Ceiling (value - 0.5);
+
+                       throw new ArgumentException ("The value '" + mode +
"' is not valid for this usage of the type MidpointRounding.", "mode"); 
               }

               [MonoTODO ("Not implemented")]
               public static double Round (double value, int digits,
MidpointRounding mode)
               {
-                       if ((mode != MidpointRounding.ToEven) && (mode !=
MidpointRounding.AwayFromZero))
-                               throw new ArgumentException ("The value '" +
mode + "' is not valid for this usage of the type MidpointRounding.",
"mode"); 
-
-                       if (mode == MidpointRounding.ToEven)
+            if (mode == MidpointRounding.ToEven)
                               return Round (value, digits);
-                       throw new NotImplementedException (); 
+                       if (mode == MidpointRounding.ToEven)
+                           throw new NotImplementedException ();
+
+                       throw new ArgumentException ("The value '" + mode +
"' is not valid for this usage of the type MidpointRounding.", "mode"); 
               }

               public static double Truncate (double d)
@@ -454,7 +453,7 @@
               }
 #endif

-               public static int Sign (decimal value)
+        public static int Sign (decimal value) 
               {
                       if (value > 0) return 1;
                       return (value == 0)? 0: -1;

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070305/c958125a/attachment.html 


More information about the Mono-devel-list mailing list