Created
August 2, 2015 05:52
-
-
Save DamianEdwards/da43888bd901f0915167 to your computer and use it in GitHub Desktop.
DateTime.ToString perf test when format string is fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Text; | |
using System.Threading; | |
namespace DateTimeFormatPerfTest | |
{ | |
public class Program | |
{ | |
private static object _locker = new object(); | |
private static DateTime _now = DateTime.Now; | |
private static Timer _timer = new Timer(UpdateNow, null, 1000, 1000); | |
private static void UpdateNow(object state) | |
{ | |
lock (_locker) | |
{ | |
_now = DateTime.Now; | |
} | |
} | |
public void Main(string[] args) | |
{ | |
var iterations = 5000000; | |
var sw = new Stopwatch(); | |
sw.Start(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
DateTimeToString(_now); | |
} | |
sw.Stop(); | |
Console.WriteLine($"{nameof(DateTimeToString)} ({Encoding.UTF8.GetString(DateTimeToString(_now))}) completed {iterations} iterations in {sw.Elapsed}"); | |
sw.Restart(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
DateTimeToStringFast(_now); | |
} | |
sw.Stop(); | |
Console.WriteLine($"{nameof(DateTimeToStringFast)} ({Encoding.UTF8.GetString(DateTimeToStringFast(_now))}) completed {iterations} iterations in {sw.Elapsed}"); | |
_timer.Dispose(); | |
} | |
private static string _format = "yyyyMMddTHHmmssZ"; | |
private byte[] DateTimeToString(DateTime dateTime) | |
{ | |
string dateTimeString; | |
lock (_locker) | |
{ | |
dateTimeString = dateTime.ToString(_format); | |
} | |
return Encoding.UTF8.GetBytes(dateTimeString); | |
} | |
private static byte[] _numbers = new[] { | |
Convert.ToByte('0'), | |
Convert.ToByte('1'), | |
Convert.ToByte('2'), | |
Convert.ToByte('3'), | |
Convert.ToByte('4'), | |
Convert.ToByte('5'), | |
Convert.ToByte('6'), | |
Convert.ToByte('7'), | |
Convert.ToByte('8'), | |
Convert.ToByte('9'), | |
}; | |
private static byte[] _specialChars = new byte[] { Convert.ToByte('T'), Convert.ToByte('Z') }; | |
private byte[] DateTimeToStringFast(DateTime dateTime) | |
{ | |
var result = new byte[_format.Length]; | |
int yearInt; | |
int monthInt; | |
int dayInt; | |
int hourInt; | |
int minuteInt; | |
int secondInt; | |
lock (_locker) | |
{ | |
yearInt = dateTime.Year; | |
monthInt = dateTime.Month; | |
dayInt = dateTime.Day; | |
hourInt = dateTime.Hour; | |
minuteInt = dateTime.Minute; | |
secondInt = dateTime.Second; | |
} | |
// Year | |
var yearThousands = (int)Math.Floor(yearInt / 1000d) * 1000; | |
var yearHundreds = (int)Math.Floor((yearInt - yearThousands) / 100d) * 100; | |
var yearTens = (int)Math.Floor((yearInt - yearThousands - yearHundreds) / 10d) * 10; | |
var yearSingles = yearInt - yearThousands - yearHundreds - yearTens; | |
result[0] = _numbers[yearThousands / 1000]; | |
result[1] = _numbers[yearHundreds / 100]; | |
result[2] = _numbers[yearTens / 10]; | |
result[3] = _numbers[yearSingles]; | |
// Month | |
var monthTens = (int)Math.Floor(monthInt / 10d) * 10; | |
var monthSingles = monthInt - monthTens; | |
result[4] = _numbers[monthTens / 10]; | |
result[5] = _numbers[monthSingles]; | |
// Day | |
var dayTens = (int)Math.Floor(dayInt / 10d) * 10; | |
var daySingles = dayInt - dayTens; | |
result[6] = _numbers[dayTens / 10]; | |
result[7] = _numbers[daySingles]; | |
// T | |
result[8] = _specialChars[0]; | |
// Hour | |
var hourTens = (int)Math.Floor(hourInt / 10d) * 10; | |
var hourSingles = hourInt - hourTens; | |
result[9] = _numbers[hourTens / 10]; | |
result[10] = _numbers[hourSingles]; | |
// Minute | |
var minuteTens = (int)Math.Floor(minuteInt / 10d) * 10; | |
var minuteSingles = minuteInt - minuteTens; | |
result[11] = _numbers[minuteTens / 10]; | |
result[12] = _numbers[minuteSingles]; | |
// Second | |
var secondTens = (int)Math.Floor(secondInt / 10d) * 10; | |
var secondSingles = secondInt - secondTens; | |
result[13] = _numbers[secondTens / 10]; | |
result[14] = _numbers[secondSingles]; | |
// Z | |
result[15] = _specialChars[1]; | |
return result; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DateTimeToString (20150801T225229Z) completed 5000000 iterations in 00:00:04.8045709 | |
DateTimeToStringFast (20150801T225231Z) completed 5000000 iterations in 00:00:01.8000201 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment