Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- logEntryTime.ToString("dd.MM.yy HH:mm:ss:fff")
- public static string FormatDateTime(DateTime dt)
- {
- char[] chars = new char[21];
- Write2Chars(chars, 0, dt.Day);
- chars[2] = '.';
- Write2Chars(chars, 3, dt.Month);
- chars[5] = '.';
- Write2Chars(chars, 6, dt.Year % 100);
- chars[8] = ' ';
- Write2Chars(chars, 9, dt.Hour);
- chars[11] = ' ';
- Write2Chars(chars, 12, dt.Minute);
- chars[14] = ' ';
- Write2Chars(chars, 15, dt.Second);
- chars[17] = ' ';
- Write2Chars(chars, 18, dt.Millisecond / 10);
- chars[20] = Digit(dt.Millisecond % 10);
- return new string(chars);
- }
- private static Write2Chars(char[] chars, int value, int offset)
- {
- chars[offset] = Digit(value / 10);
- chars[offset+1] = Digit(value % 10);
- }
- private static char Digit(int value)
- {
- return (char) (value + '0');
- }
- Basic: 2342ms
- Custom: 1319ms
- Basic: 2275ms
- Custom: 839ms
- using System.Diagnostics;
- using System;
- using System.IO;
- static class Program
- {
- static void Main()
- {
- DateTime when = DateTime.Now;
- const int LOOP = 1000000;
- Stopwatch basic = Stopwatch.StartNew();
- using (TextWriter tw = new StreamWriter("basic.txt"))
- {
- for (int i = 0; i < LOOP; i++)
- {
- tw.Write(when.ToString("dd.MM.yy HH:mm:ss:fff"));
- }
- }
- basic.Stop();
- Console.WriteLine("Basic: " + basic.ElapsedMilliseconds + "ms");
- char[] buffer = new char[100];
- Stopwatch custom = Stopwatch.StartNew();
- using (TextWriter tw = new StreamWriter("custom.txt"))
- {
- for (int i = 0; i < LOOP; i++)
- {
- WriteDateTime(tw, when, buffer);
- }
- }
- custom.Stop();
- Console.WriteLine("Custom: " + custom.ElapsedMilliseconds + "ms");
- }
- static void WriteDateTime(TextWriter output, DateTime when, char[] buffer)
- {
- buffer[2] = buffer[5] = '.';
- buffer[8] = ' ';
- buffer[11] = buffer[14] = buffer[17] = ':';
- Write2(buffer, when.Day, 0);
- Write2(buffer, when.Month, 3);
- Write2(buffer, when.Year % 100, 6);
- Write2(buffer, when.Hour, 9);
- Write2(buffer, when.Minute, 12);
- Write2(buffer, when.Second, 15);
- Write3(buffer, when.Millisecond, 18);
- output.Write(buffer, 0, 21);
- }
- static void Write2(char[] buffer, int value, int offset)
- {
- buffer[offset++] = (char)('0' + (value / 10));
- buffer[offset] = (char)('0' + (value % 10));
- }
- static void Write3(char[] buffer, int value, int offset)
- {
- buffer[offset++] = (char)('0' + (value / 100));
- buffer[offset++] = (char)('0' + ((value / 10) % 10));
- buffer[offset] = (char)('0' + (value % 10));
- }
- }
Add Comment
Please, Sign In to add comment