2004-10-17 Ben Maurer <bmaurer@ximian.com>
authorBen Maurer <benm@mono-cvs.ximian.com>
Sun, 17 Oct 2004 15:10:03 +0000 (15:10 -0000)
committerBen Maurer <benm@mono-cvs.ximian.com>
Sun, 17 Oct 2004 15:10:03 +0000 (15:10 -0000)
* DateTime.cs (ZeroPad): Use unsafe code to speed this up. We
avoid entering slow integer formatting code.

(_ToString): Use ZeroPad here when possible, as it is faster.

svn path=/trunk/mcs/; revision=35057

mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/DateTime.cs

index 1f90b7ca9cdb5f411aa20406832d2e9b2e184d04..05d8064d6fa917269d4bd38fdd69df9d861a9a46 100644 (file)
@@ -1,3 +1,10 @@
+2004-10-17  Ben Maurer  <bmaurer@ximian.com>
+
+       * DateTime.cs (ZeroPad): Use unsafe code to speed this up. We
+       avoid entering slow integer formatting code.
+
+       (_ToString): Use ZeroPad here when possible, as it is faster.
+
 2004-10-11  Martin Baulig  <martin@ximian.com>
 
        * Environment.cs: Bump corlib version to 28.
index ee7b8b3befa61adc99eb444057ba2031285c3a63..2acbd6dd683b2b5cb5f00078b232898cefd9f75f 100644 (file)
@@ -1670,7 +1670,7 @@ namespace System
                                        if (tokLen <= 2)
                                                ZeroPad (result, dfi.Calendar.GetYear (this) % 100, tokLen);
                                        else
-                                               result.Append (dfi.Calendar.GetYear (this).ToString ("D" + (tokLen == 3 ? 3 : 4)));
+                                               ZeroPad (result, dfi.Calendar.GetYear (this), (tokLen == 3 ? 3 : 4));
 
                                        break;
                                case 'g':
@@ -1760,9 +1760,22 @@ namespace System
                        throw new FormatException("Un-ended quote");
                }
                
-               static void ZeroPad (StringBuilder output, int digits, int padding)
+               static unsafe void ZeroPad (StringBuilder output, int digits, int len)
                {
-                       output.Append (digits.ToString (new string ('0', padding)));
+                       // more than enough for an int
+                       char* buffer = stackalloc char [16];
+                       int pos = 16;
+                       
+                       do {
+                               buffer [-- pos] = (char) ('0' + digits % 10);
+                               digits /= 10;
+                               len --;
+                       } while (digits > 0);
+                       
+                       while (len -- > 0)
+                               buffer [-- pos] = '0';
+                       
+                       output.Append (new string (buffer, pos, 16 - pos));
                }
 
                public string ToString (string format, IFormatProvider fp)