Delphi高效定制格式的FormatDateTime

Posted marklove

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi高效定制格式的FormatDateTime相关的知识,希望对你有一定的参考价值。

本人根据歼10博主的此博文的思路进行改进,目的是高效的实现FormatDateTime(‘YYYY-MM-DD HH:NN:SS.ZZZ‘, Now);

在DelphiXE3 32Bits环境测试10,000,000次,

Delphi自带的FormatDateTime  = 20405 ms

歼10的优化函数  = 2683 ms

本文优化函数 = 1851 ms

在DelphiXE3 64Bits环境测试10,000,000次,

Delphi自带的FormatDateTime  = 18782 ms

歼10的优化函数  = 2091 ms

本文优化函数 = 1302ms

 

ideChar =
    00010203040506070809101112131415161718192021222324252627282930 +
    313233343536373839404142434445464748495051525354555657585960 +
    6162636465666768697071727374757677787980 +
    81828384858687888990919293949596979899;
  strPattern10: PWideChar = 0#01#02#03#04#05#06#07#08#09#0;
  strPatternYear: PWideChar =
    201420152016201720182019202020212022202320242025202620272028202920302031 +
    2032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056 +
    2057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081 +
    2082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106 +
    2107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921392131;
  strPatternMonth: PWideChar =
    -00--01--02--03--04--05--06--07--08--09--10--11--12-;
  strPatternHour: PWideChar =
     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: PWideChar =
    :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.;
 
procedure sfNowToBuf(const OutBuf: PWideChar; BufSize: Integer);
var
  Year, Month, Day, HH, MM, SS, ZZZ: WORD;
  P: PUInt32;
  I: Integer;
  SystemTime: TSystemTime;
  lvBuf: array[0..23] of Widechar;
begin
  if BufSize <= 0 then Exit;
  P := @lvBuf[0]; // OutBuff;
 
  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)^ := PUInt64Array(strPatternYear)[Year];  Inc(PUInt64(P));
  //Month
  PUInt64(P)^ := PUInt64Array(strPatternMonth)[Month];  Inc(PUInt64(P));
  //Day
  P^ := PUInt32Array(strPatternHandred)[Day];  Inc(P);
  //HH
  PUInt64(P)^ := PUInt64Array(strPatternHour)[HH];  Inc(PUInt64(P));
  //MM
  P^ := PUInt32Array(strPatternHandred)[MM];  Inc(P);
  //SS
  PUInt64(P)^ := PUInt64Array(strPatternSecond)[SS];  Inc(PUInt64(P));
  //ZZZ
  I := (ZZZ div 10);
  P^ := PUInt32Array(strPatternHandred)[I];  Inc(P);
  I := (ZZZ mod 10);
  P^ := PUInt32Array(strPattern10)[I];
 
  if BufSize > 23 then BufSize := 23;
  Move(lvBuf, OutBuf^, BufSize*Sizeof(WideChar));
end;

极限优化,进一步做了提速。
64位 XE10.1,Debug,运行10,000,000次结果:
FormatDateTime:18596 ms
歼10:2464 ms
楼顶:921 ms
本楼:795 ms

64位 XE10.1,Release,运行10,000,000次结果:
FormatDateTime:19437 ms
歼10:1311 ms
楼顶:795 ms
本楼:671 ms

32位 XE10.1 下本楼跟楼顶的速度一样。

程序如下:

type
PUInt64 = ^UInt64;
UInt64Array = array[0..0] of UInt64;
PUInt64Array = ^UInt64Array;
 
const
strPatternYear: PWideChar =
201420152016201720182019202020212022202320242025202620272028202920302031 +
2032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056 +
2057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081 +
2082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106 +
2107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921392131;
strPatternMonth: PWideChar =
-00--01--02--03--04--05--06--07--08--09--10--11--12-;
strPatternHour: PWideChar =
 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: PWideChar =
: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.;
 
strPatternDDH: PWideChar =
00 000 100 200 300 400 500 600 700 800 901 001 101 201 301 401 501 601 701 801 9+
02 002 102 202 302 402 502 602 702 802 903 003 103 203 303 403 503 603 703 803 9+
04 004 104 204 304 404 504 604 704 804 905 005 105 205 305 405 505 605 705 805 9+
06 006 106 206 306 406 506 606 706 806 907 007 107 207 307 407 507 607 707 807 9+
08 008 108 208 308 408 508 608 708 808 909 009 109 209 309 409 509 609 709 809 9+
10 010 110 210 310 410 510 610 710 810 911 011 111 211 311 411 511 611 711 811 9+
12 012 112 212 312 412 512 612 712 812 913 013 113 213 313 413 513 613 713 813 9+
14 014 114 214 314 414 514 614 714 814 915 015 115 215 315 415 515 615 715 815 9+
16 016 116 216 316 416 516 616 716 816 917 017 117 217 317 417 517 617 717 817 9+
18 018 118 218 318 418 518 618 718 818 919 019 119 219 319 419 519 619 719 819 9+
20 020 120 220 320 420 520 620 720 820 921 021 121 221 321 421 521 621 721 821 9+
22 022 122 222 322 422 522 622 722 822 923 023 123 223 323 423 523 623 723 823 9+
24 024 124 224 324 424 524 624 724 824 925 025 125 225 325 425 525 625 725 825 9+
26 026 126 226 326 426 526 626 726 826 927 027 127 227 327 427 527 627 727 827 9+
28 028 128 228 328 428 528 628 728 828 929 029 129 229 329 429 529 629 729 829 9+
30 030 130 230 330 430 530 630 730 830 931 031 131 231 331 431 531 631 731 831 9+
32 032 132 232 332 432 532 632 732 832 933 033 133 233 333 433 533 633 733 833 9+
34 034 134 234 334 434 534 634 734 834 935 035 135 235 335 435 535 635 735 835 9+
36 036 136 236 336 436 536 636 736 836 937 037 137 237 337 437 537 637 737 837 9+
38 038 138 238 338 438 538 638 738 838 939 039 139 239 339 439 539 639 739 839 9+
40 040 140 240 340 440 540 640 740 840 941 041 141 241 341 441 541 641 741 841 9+
42 042 142 242 342 442 542 642 742 842 943 043 143 243 343 443 543 643 743 843 9+
44 044 144 244 344 444 544 644 744 844 945 045 145 245 345 445 545 645 745 845 9+
46 046 146 246 346 446 546 646 746 846 947 047 147 247 347 447 547 647 747 847 9+
48 048 148 248 348 448 548 648 748 848 949 049 149 249 349 449 549 649 749 849 9+
50 050 150 250 350 450 550 650 750 850 951 051 151 251 351 451 551 651 751 851 9+
52 052 152 252 352 452 552 652 752 852 953 053 153 253 353 453 553 653 753 853 9+
54 054 154 254 354 454 554 654 754 854 955 055 155 255 355 455 555 655 755 855 9+
56 056 156 256 356 456 556 656 756 856 957 057 157 257 357 457 557 657 757 857 9+
58 058 158 258 358 458 558 658 758 858 959 059 159 259 359 459 559 659 759 859 9;
 
strPatternHMM: PWideChar =
0:000:010:020:030:040:050:060:070:080:090:100:110:120:130:140:150:160:170:180:190:200:210:220:230:240:250:260:270:280:29+
0:300:310:320:330:340:350:360:370:380:390:400:410:420:430:440:450:460:470:480:490:500:510:520:530:540:550:560:570:580:59+
1:001:011:021:031:041:051:061:071:081:091:101:111:121:131:141:151:161:171:181:191:201:211:221:231:241:251:261:271:281:29+
1:301:311:321:331:341:351:361:371:381:391:401:411:421:431:441:451:461:471:481:491:501:511:521:531:541:551:561:571:581:59+
2:002:012:022:032:042:052:062:072:082:092:102:112:122:132:142:152:162:172:182:192:202:212:222:232:242:252:262:272:282:29+
2:302:312:322:332:342:352:362:372:382:392:402:412:422:432:442:452:462:472:482:492:502:512:522:532:542:552:562:572:582:59+
3:003:013:023:033:043:053:063:073:083:093:103:113:123:133:143:153:163:173:183:193:203:213:223:233:243:253:263:273:283:29+
3:303:313:323:333:343:353:363:373:383:393:403:413:423:433:443:453:463:473:483:493:503:513:523:533:543:553:563:573:583:59+
4:004:014:024:034:044:054:064:074:084:094:104:114:124:134:144:154:164:174:184:194:204:214:224:234:244:254:264:274:284:29+
4:304:314:324:334:344:354:364:374:384:394:404:414:424:434:444:454:464:474:484:494:504:514:524:534:544:554:564:574:584:59+
5:005:015:025:035:045:055:065:075:085:095:105:115:125:135:145:155:165:175:185:195:205:215:225:235:245:255:265:275:285:29+
5:305:315:325:335:345:355:365:375:385:395:405:415:425:435:445:455:465:475:485:495:505:515:525:535:545:555:565:575:585:59+
6:006:016:026:036:046:056:066:076:086:096:106:116:126:136:146:156:166:176:186:196:206:216:226:236:246:256:266:276:286:29+
6:306:316:326:336:346:356:366:376:386:396:406:416:426:436:446:456:466:476:486:496:506:516:526:536:546:556:566:576:586:59+
7:007:017:027:037:047:057:067:077:087:097:107:117:127:137:147:157:167:177:187:197:207:217:227:237:247:257:267:277:287:29+
7:307:317:327:337:347:357:367:377:387:397:407:417:427:437:447:457:467:477:487:497:507:517:527:537:547:557:567:577:587:59+
8:008:018:028:038:048:058:068:078:088:098:108:118:128:138:148:158:168:178:188:198:208:218:228:238:248:258:268:278:288:29+
8:308:318:328:338:348:358:368:378:388:398:408:418:428:438:448:458:468:478:488:498:508:518:528:538:548:558:568:578:588:59+
9:009:019:029:039:049:059:069:079:089:099:109:119:129:139:149:159:169:179:189:199:209:219:229:239:249:259:269:279:289:29+
9:309:319:329:339:349:359:369:379:389:399:409:419:429:439:449:459:469:479:489:499:509:519:529:539:549:559:569:579:589:59;
 
strPatternZZZ: PWideChar =
000#0001#0002#0003#0004#0005#0006#0007#0008#0009#0010#0011#0012#0013#0014#0015#0016#0017#0018#0019#0020#0021#0022#0023#0024#0+
025#0026#0027#0028#0029#0030#0031#0032#0033#0034#0035#0036#0037#0038#0039#0040#0041#0042#0043#0044#0045#0046#0047#0048#0049#0+
050#0051#0052#0053#0054#0055#0056#0057#0058#0059#0060#0061#0062#0063#0064#0065#0066#0067#0068#0069#0070#0071#0072#0073#0074#0+
075#0076#0077#0078#0079#0080#0081#0082#0083#0084#0085#0086#0087#0088#0089#0090#0091#0092#0093#0094#0095#0096#0097#0098#0099#0+
100#0101#0102#0103#0104#0105#0106#0107#0108#0109#0110#0111#0112#0113#0114#0115#0116#0117#0118#0119#0120#0121#0122#0123#0124#0+
125#0126#0127#0128#0129#0130#0131#0132#0133#0134#0135#0136#0137#0138#0139#0140#0141#0142#0143#0144#0145#0146#0147#0148#0149#0+
150#0151#0152#0153#0154#0155#0156#0157#0158#0159#0160#0161#0162#0163#0164#0165#0166#0167#0168#0169#0170#0171#0172#0173#0174#0+
175#0176#0177#0178#0179#0180#0181#0182#0183#0184#0185#0186#0187#0188#0189#0190#0191#0192#0193#0194#0195#0196#0197#0198#0199#0+
200#0201#0202#0203#0204#0205#0206#0207#0208#0209#0210#0211#0212#0213#0214#0215#0216#0217#0218#0219#0220#0221#0222#0223#0224#0+
225#0226#0227#0228#0229#0230#0231#0232#0233#0234#0235#0236#0237#0238#0239#0240#0241#0242#0243#0244#0245#0246#0247#0248#0249#0+
250#0251#0252#0253#0254#0255#0256#0257#0258#0259#0260#0261#0262#0263#0264#0265#0266#0267#0268#0269#0270#0271#0272#0273#0274#0+
275#0276#0277#0278#0279#0280#0281#0282#0283#0284#0285#0286#0287#0288#0289#0290#0291#0292#0293#0294#0295#0296#0297#0298#0299#0+
300#0301#0302#0303#0304#0305#0306#0307#0308#0309#0310#0311#0312#0313#0314#0315#0316#0317#0318#0319#0320#0321#0322#0323#0324#0+
325#0326#0327#0328#0329#0330#0331#0332#0333#0334#0335#0336#0337#0338#0339#0340#0341#0342#0343#0344#0345#0346#0347#0348#0349#0+
350#0351#0352#0353#0354#0355#0356#0357#0358#0359#0360#0361#0362#0363#0364#0365#0366#0367#0368#0369#0370#0371#0372#0373#0374#0+
375#0376#0377#0378#0379#0380#0381#0382#0383#0384#0385#0386#0387#0388#0389#0390#0391#0392#0393#0394#0395#0396#0397#0398#0399#0+
400#0401#0402#0403#0404#0405#0406#0407#0408#0409#0410#0411#0412#0413#0414#0415#0416#0417#0418#0419#0420#0421#0422#0423#0424#0+
425#0426#0427#0428#0429#0430#0431#0432#0433#0434#0435#0436#0437#0438#0439#0440#0441#0442#0443#0444#0445#0446#0447#0448#0449#0+
450#0451#0452#0453#0454#0455#0456#0457#0458#0459#0460#0461#0462#0463#0464#0465#0466#0467#0468#0469#0470#0471#0472#0473#0474#0+
475#0476#0477#0478#0479#0480#0481#0482#0483#0484#0485#0486#0487#0488#0489#0490#0491#0492#0493#0494#0495#0496#0497#0498#0499#0+
500#0501#0502#0503#0504#0505#0506#0507#0508#0509#0510#0511#0512#0513#0514#0515#0516#0517#0518#0519#0520#0521#0522#0523#0524#0+
525#0526#0527#0528#0529#0530#0531#0532#0533#0534#0535#0536#0537#0538#0539#0540#0541#0542#0543#0544#0545#0546#0547#0548#0549#0+
550#0551#0552#0553#0554#0555#0556#0557#0558#0559#0560#0561#0562#0563#0564#0565#0566#0567#0568#0569#0570#0571#0572#0573#0574#0+
575#0576#0577#0578#0579#0580#0581#0582#0583#0584#0585#0586#0587#0588#0589#0590#0591#0592#0593#0594#0595#0596#0597#0598#0599#0+
600#0601#0602#0603#0604#0605#0606#0607#0608#0609#0610#0611#0612#0613#0614#0615#0616#0617#0618#0619#0620#0621#0622#0623#0624#0+
625#0626#0627#0628#0629#0630#0631#0632#0633#0634#0635#0636#0637#0638#0639#0640#0641#0642#0643#0644#0645#0646#0647#0648#0649#0+
650#0651#0652#0653#0654#0655#0656#0657#0658#0659#0660#0661#0662#0663#0664#0665#0666#0667#0668#0669#0670#0671#0672#0673#0674#0+
675#0676#0677#0678#0679#0680#0681#0682#0683#0684#0685#0686#0687#0688#0689#0690#0691#0692#0693#0694#0695#0696#0697#0698#0699#0+
700#0701#0702#0703#0704#0705#0706#0707#0708#0709#0710#0711#0712#0713#0714#0715#0716#0717#0718#0719#0720#0721#0722#0723#0724#0+
725#0726#0727#0728#0729#0730#0731#0732#0733#0734#0735#0736#0737#0738#0739#0740#0741#0742#0743#0744#0745#0746#0747#0748#0749#0+
750#0751#0752#0753#0754#0755#0756#0757#0758#0759#0760#0761#0762#0763#0764#0765#0766#0767#0768#0769#0770#0771#0772#0773#0774#0+
775#0776#0777#0778#0779#0780#0781#0782#0783#0784#0785#0786#0787#0788#0789#0790#0791#0792#0793#0794#0795#0796#0797#0798#0799#0+
800#0801#0802#0803#0804#0805#0806#0807#0808#0809#0810#0811#0812#0813#0814#0815#0816#0817#0818#0819#0820#0821#0822#0823#0824#0+
825#0826#0827#0828#0829#0830#0831#0832#0833#0834#0835#0836#0837#0838#0839#0840#0841#0842#0843#0844#0845#0846#0847#0848#0849#0+
850#0851#0852#0853#0854#0855#0856#0857#0858#0859#0860#0861#0862#0863#0864#0865#0866#0867#0868#0869#0870#0871#0872#0873#0874#0+
875#0876#0877#0878#0879#0880#0881#0882#0883#0884#0885#0886#0887#0888#0889#0890#0891#0892#0893#0894#0895#0896#0897#0898#0899#0+
900#0901#0902#0903#0904#0905#0906#0907#0908#0909#0910#0911#0912#0913#0914#0915#0916#0917#0918#0919#0920#0921#0922#0923#0924#0+
925#0926#0927#0928#0929#0930#0931#0932#0933#0934#0935#0936#0937#0938#0939#0940#0941#0942#0943#0944#0945#0946#0947#0948#0949#0+
950#0951#0952#0953#0954#0955#0956#0957#0958#0959#0960#0961#0962#0963#0964#0965#0966#0967#0968#0969#0970#0971#0972#0973#0974#0+
975#0976#0977#0978#0979#0980#0981#0982#0983#0984#0985#0986#0987#0988#0989#0990#0991#0992#0993#0994#0995#0996#0997#0998#0999#0;
 
 
procedure sfNowToBuf(const OutBuf: PWideChar; BufSize: Integer);
var
Year, Month, Day, HH, MM, SS, ZZZ, DDH, HMM: WORD;
P: PUInt64;
I: Integer;
SystemTime: TSystemTime;
lvBuf: array[0..23] of Widechar;
begin
if BufSize < 24 then Exit;
P := Pointer(OutBuf);
 
GetLocalTime(SystemTime);
Year := SystemTime.wYear - 2014;
Month := SystemTime.wMonth;
DDH := SystemTime.wDay * 10 + SystemTime.wHour div 10;
HMM := (SystemTime.wHour mod 10) * 60 + SystemTime.wMinute;
SS := SystemTime.wSecond;
ZZZ := SystemTime.wMilliseconds;
 
//Year
P^ := PUInt64Array(strPatternYear)[Year]; Inc(P);
//Month
P^ := PUInt64Array(strPatternMonth)[Month]; Inc(P);
//DD H
P^ := PUInt64Array(strPatternDDH)[DDH]; Inc(P);
//H:MM
P^ := PUInt64Array(strPatternHMM)[HMM]; Inc(P);
//:SS.
P^ := PUInt64Array(strPatternSecond)[SS]; Inc(P);
//ZZZ
P^ := PUInt64Array(strPatternZZZ)[ZZZ]; ;
 
// if BufSize > 24 then BufSize := 24;
// Move(lvBuf, OutBuf^, BufSize*Sizeof(WideChar));
end;

 










以上是关于Delphi高效定制格式的FormatDateTime的主要内容,如果未能解决你的问题,请参考以下文章

Delphi 高效读写锁

浅谈Delphi高效使用TreeView

Delphi中的DBGrid控件

如何更高效地定制你的bootstrap

Delphi中统一显示表格字段名的高效方法

专业又高效的APP外包定制平台