Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor for Speed: Convert To a Date

I work for myself, I am a self-employed coder and as a result I don't have the luxury of code reviews or the ability to improve based upon peer programming. I am going to use this as an exercise to see if the StackOverflow community might help to review a simple method which i've written;

    internal static DateTime CONVERT_To_DateTime(int binDate)
    {
        //   3/10/2008 = 1822556159
        //   2/10/2008 = 1822523391
        //   1/10/2008 = 1822490623
        //   30/09/2008 = 1822392319
        //   29/09/2008 = 1822359551

        //   September 30th 2008
        //   1822392319 = 0x6c9f7fff
        //   0x6c = 108 = 2008 (based on 1900 start date)
        //   0x9 = 9 = September
        //   0xf7fff - take top 5 bits = 0x1e = 30

        //   October 1st 2008
        //   1822490623 = 0x6ca0ffff
        //   0 x6c = 108 = 2008
        //   0 xa = 10 = October
        //   0x0ffff  - take top 5 bits = 0x01 = 1

        //   OR using Binary (used by this function)
        //   a = 1822556159 (3/10/2008)
        //   1101100 1010 00011 111111111111111

        //   b = 1822523391 (2/10/2008)
        //   1101100 1010 00010 111111111111111

        //   c = 1822490623 (1/10/2008)
        //   1101100 1010 00001 111111111111111

        //   D = 1822392319 (30/09/2008)
        //   1101100 1001 11110 111111111111111

        //   Excess 111111 are probably used for time/seconds which
        //   we do not care for at the current time

        var BaseYear = 1900;

        //  Dump the long date to binary
        var strBinary = Convert.ToString(binDate);

        //  Calculate the year
        var strBYear = strBinary.Substring(0, 7);
        var iYear = Convert.ToInt32(strBYear, 2) + BaseYear;

        //  Calculate the month
        var strBMonth = strBinary.Substring(7, 4);
        var iMonth = Convert.ToInt32(strBMonth, 2);

        //  Calculate the day
        var strBDay = strBinary.Substring(11, 5);
        var iDay = Convert.ToInt32(strBDay, 2);

        // ensure that month and day have two digits
        var strDay = iDay < 10 ? "0" + iDay : iDay.ToString();
        var strMonth = iMonth < 10 ? "0" + iMonth : iMonth.ToString();

        //  Build the final date
        var convertedDate = iYear + strMonth + strDay;

        return DateTime.ParseExact(convertedDate, "yyyyMMdd", null);
    }

This is a method that takes a numeric representation of a date and converts it to a DateTime DataType. I would like the method to be reviewed to acheive the fastest possible execution time because it's being executed within a loop.

Any comments on the method is appreciated as this will be an exercise for me. i look forward to some responses.

like image 668
Phillis Avatar asked Feb 03 '10 14:02

Phillis


People also ask

How do I convert a character to a date in R?

You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.

What is POSIXct format in R?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.


3 Answers

Instead of converting to a string, then to integers, then to string, then to date, just get the integers by shifting and masking, and create the DateTime value directly from the integer values:

binDate >>= 15;
int day = binDate & 31;
binDate >>= 5;
int month = binDate & 15;
binDate >>= 8;
int year = binDate + 1900;
return new DateTime(year, month, day);
like image 157
Guffa Avatar answered Nov 15 '22 02:11

Guffa


You're doing string manipulations. This is true performance killer when used in tight loops.

    static DateTime ToDateTime(int value)
    {
        var year = (int)((value & 0xff000000) >> 24);
        var month = (value & 0xf00000) >> 20;
        var day = (value & (0xf8000)) >> 15;

        return new DateTime(1900 + year, month, day);
    }

Here's how you do that. First, take 1822490623 and convert it to binary:

0110 1100 1010 0000 1111 1111 1111 1111

This is a mask for year:

f    f    0    0    0    0    0    0 

This is for month:

0    0    f    0    0    0    0    0

And this is for day:

0    0    0    f    8    0    0    0

"Year" value has to be shifted right by 6 * 4 bits, "month" - by 5 * 4, and "day" - by 3 * 4 + 3 bits.

like image 41
Anton Gogolev Avatar answered Nov 15 '22 04:11

Anton Gogolev


Welcome to the community, Phillis. :)

Anton is correct, your string manipulations are going to be slow. Because it looks like you're using the parameter as a bitfield, I'd suggest looking into the various (much faster) bit operators: <<, >>, &, |, and ~. It looks like you're trying to do binary manipulation, so use the operators built for it.

E.g. (untested, just off the cuff):

You start with a value of 0x6c9f7fff. The high order byte makes up the year. To mask out everything that isn't the year, do something like:

int year = ((binDate & 0xFF000000) >> 24) + BaseYear;

Likewise, the next 4 bits are the month, so:

int month = (binDate & 0x00F00000) >> 20;
int date = (binDate & 0x000F8000) >> 15;
return new DateTime(year, month, date);
like image 43
Greg D Avatar answered Nov 15 '22 04:11

Greg D