Skip to content

Instantly share code, notes, and snippets.

@dradovic
Created May 15, 2015 07:37
Show Gist options
  • Save dradovic/b5578a4bcb1e9d182cc9 to your computer and use it in GitHub Desktop.
Save dradovic/b5578a4bcb1e9d182cc9 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
const int count = 1000000;
DateTime date = new DateTime(2015, 5, 14);
int dateAsInt;
DateTime deserialized;
Stopwatch watch = new Stopwatch();
watch.Restart();
for (int i = 0; i < count; i++)
{
dateAsInt = DateToInt(date);
deserialized = IntToDate(dateAsInt);
Debug.Assert(deserialized.Equals(date));
}
watch.Stop();
Console.WriteLine("BitVector: {0}", watch.ElapsedMilliseconds);
watch.Restart();
for (int i = 0; i < count; i++)
{
dateAsInt = (int)(date.Ticks / TimeSpan.TicksPerDay);
long ticks = dateAsInt * System.TimeSpan.TicksPerDay;
deserialized = new DateTime(ticks);
Debug.Assert(deserialized.Equals(date));
}
watch.Stop();
Console.WriteLine("Ticks division: {0}", watch.ElapsedMilliseconds);
Console.ReadLine();
}
private static readonly BitVector32.Section YearSection = BitVector32.CreateSection(9999);
private static readonly BitVector32.Section MonthSection = BitVector32.CreateSection(12, YearSection);
private static readonly BitVector32.Section DaySection = BitVector32.CreateSection(31, MonthSection);
private static int DateToInt(DateTime date)
{
var bv = new BitVector32(0);
bv[YearSection] = date.Year;
bv[MonthSection] = date.Month;
bv[DaySection] = date.Day;
return bv.Data;
}
private static DateTime IntToDate(int date)
{
var bv = new BitVector32(date);
return new DateTime(bv[YearSection], bv[MonthSection], bv[DaySection]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment