本人根據殲10博主的此博文的思路進行改進,目的是高效的實現FormatDateTime('YYYY-MM-DD HH:NN:SS.ZZZ', Now);
在DelphiXE3環境測試10,000,000次,
Delphi自帶的FormatDateTime = 20405 ms
殲10的優化函數 = 2683 ms
本文優化函數 = 1903 ms
在DelphiXE3 環境測試10,000,000次,
Delphi自帶的FormatDateTime = 18782 ms
殲10的優化函數 = 2091 ms
本文優化函數 = 1357 ms
type
UInt32 = LongWord;
UInt32Array = array[0..0] of UInt32;
PUInt32Array = ^UInt32Array;
PUInt32 = ^UInt32;
PUInt64 = ^UInt64;
UInt64Array = array[0..0] of UInt64;
PUInt64Array = ^UInt64Array;
procedure sfNowToBuf5(const OutBuf: PWideChar; BufSize: Integer);
const
strPatternHandred: Widestring =
'00010203040506070809101112131415161718192021222324252627282930' +
'313233343536373839404142434445464748495051525354555657585960' +
'6162636465666768697071727374757677787980' +
'81828384858687888990919293949596979899';
strPattern10: Widestring = '0'#0'1'#0'2'#0'3'#0'4'#0'5'#0'6'#0'7'#0'8'#0'9'#0;
strPatternYear: Widestring =
'201420152016201720182019202020212022202320242025202620272028202920302031' +
'2032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056' +
'2057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081' +
'2082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106';
strPatternMonth: WideString =
'-00--01--02--03--04--05--06--07--08--09--10--11--12-';
strPatternHour: WideString =
' 00: 01: 02: 03: 04: 05: 06: 07: 08: 09: 10: 11: 12: 13: 14: 15: ' +
'16: 17: 18: 19: 20: 21: 22: 23:';
strPatternSecond: WideString =
':00.:01.:02.:03.:04.:05.:06.:07.:08.:09.:10.:11.:12.:13.:14.:15.:' +
'16.:17.:18.:19.:20.:21.:22.:23.:24.:25.:26.:27.:28.:29.:30.:31.:32.:33.:34.:35.:36.:37.:38.:' +
'39.:40.:41.:42.:43.:44.:45.:46.:47.:48.:49.:50.:51.:52.:53.:54.:55.:56.:57.:58.:59.';
var
Year, Month, Day, HH, MM, SS, ZZZ: WORD;
Q: PWideChar;
P: PUInt32;
strHandred: PUInt32Array;
strYear, strMonth, strHour, strSecond: PUInt64Array;
str10: PUInt32Array;
I, J: Integer;
SystemTime: TSystemTime;
lvBuf: array[0..23] of Widechar;
begin
if BufSize <= 0 then
Exit;
strHandred := PUInt32Array(PWideChar(strPatternHandred));
str10 := PUInt32Array(PWideChar(strPattern10));
strYear := PUInt64Array(PWideChar(strPatternYear));
strMonth := PUInt64Array(PWideChar(strPatternMonth));
strHour := PUInt64Array(PWideChar(strPatternHour));
strSecond := PUInt64Array(PWideChar(strPatternSecond));
P := @lvBuf[0]; // OutBuff;
FillChar(lvBuf, sizeof(lvBuf), 0);
GetLocalTime(SystemTime);
Year := SystemTime.wYear - 2014;
Month := SystemTime.wMonth;
Day := SystemTime.wDay;
HH := SystemTime.wHour;
MM := SystemTime.wMinute;
SS := SystemTime.wSecond;
ZZZ := SystemTime.wMilliseconds;
//Year
PUInt64(P)^ := strYear[Year]; Inc(PUInt64(P));
//Month
PUInt64(P)^ := strMonth[Month]; Inc(PUInt64(P));
//Day
P^ := strHandred[Day]; Inc(P);
//HH
PUInt64(P)^ := strHour[HH]; Inc(PUInt64(P));
//MM
P^ := strHandred[MM]; Inc(P);
//SS
PUInt64(P)^ := strSecond[SS]; Inc(PUInt64(P));
//ZZZ
I := (ZZZ div 10);
P^ := strHandred[I]; Inc(P);
I := (ZZZ mod 10);
P^ := str10[I];
if BufSize > 23 then BufSize := 23;
Q := OutBuf;
for I := 0 to BufSize - 1 do
Q[I] := lvBuf[I]
end;