Lavoro per me stesso, sono un programmatore autonomo e di conseguenza non ho il lusso delle recensioni del codice o la possibilità di migliorare in base alla programmazione peer. Ho intenzione di utilizzare questo come un esercizio per vedere se la comunità StackOverflow potrebbe aiutare a rivedere un semplice metodo che ho scritto;Refactor per la velocità: Converti in una data
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);
}
Questo è un metodo che accetta una rappresentazione numerica di una data e lo converte in un tipo di dati DateTime. Vorrei che il metodo da recensione per acheive il più veloce tempo di esecuzione possibile perché è in esecuzione all'interno di un ciclo.
Qualsiasi commento sul metodo è apprezzato in quanto questo sarà un esercizio per me. attendo alcune risposte.
Si dovrebbe sempre contrassegnare domande come questa, con un linguaggio di programmazione, per aiutare le persone lo trovano. – unwind
le operazioni binarie diventano più veloci utilizzando un metodo bit mask e bit shift – tooleb
FYI, omettendo lo 0 iniziale nei numeri binari potrebbe confondere gli altri sviluppatori. È giusto aspettarsi che tutti i 32 bit siano rappresentati. –