• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Delphi高效定制格式的FormatDateTime

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本人根据歼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'#0'1'#0'2'#0'3'#0'4'#0'5'#0'6'#0'7'#0'8'#0'9'#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'#0'001'#0'002'#0'003'#0'004'#0'005'#0'006'#0'007'#0'008'#0'009'#0'010'#0'011'#0'012'#0'013'#0'014'#0'015'#0'016'#0'017'#0'018'#0'019'#0'020'#0'021'#0'022'#0'023'#0'024'#0+
'025'#0'026'#0'027'#0'028'#0'029'#0'030'#0'031'#0'032'#0'033'#0'034'#0'035'#0'036'#0'037'#0'038'#0'039'#0'040'#0'041'#0'042'#0'043'#0'044'#0'045'#0'046'#0'047'#0'048'#0'049'#0+
'050'#0'051'#0'052'#0'053'#0'054'#0'055'#0'056'#0'057'#0'058'#0'059'#0'060'#0'061'#0'062'#0'063'#0'064'#0'065'#0'066'#0'067'#0'068'#0'069'#0'070'#0'071'#0'072'#0'073'#0'074'#0+
'075'#0'076'#0'077'#0'078'#0'079'#0'080'#0'081'#0'082'#0'083'#0'084'#0'085'#0'086'#0'087'#0'088'#0'089'#0'090'#0'091'#0'092'#0'093'#0'094'#0'095'#0'096'#0'097'#0'098'#0'099'#0+
'100'#0'101'#0'102'#0'103'#0'104'#0'105'#0'106'#0'107'#0'108'#0'109'#0'110'#0'111'#0'112'#0'113'#0'114'#0'115'#0'116'#0'117'#0'118'#0'119'#0'120'#0'121'#0'122'#0'123'#0'124'#0+
'125'#0'126'#0'127'#0'128'#0'129'#0'130'#0'131'#0'132'#0'133'#0'134'#0'135'#0'136'#0'137'#0'138'#0'139'#0'140'#0'141'#0'142'#0'143'#0'144'#0'145'#0'146'#0'147'#0'148'#0'149'#0+
'150'#0'151'#0'152'#0'153'#0'154'#0'155'#0'156'#0'157'#0'158'#0'159'#0'160'#0'161'#0'162'#0'163'#0'164'#0'165'#0'166'#0'167'#0'168'#0'169'#0'170'#0'171'#0'172'#0'173'#0'174'#0+
'175'#0'176'#0'177'#0'178'#0'179'#0'180'#0'181'#0'182'#0'183'#0'184'#0'185'#0'186'#0'187'#0'188'#0'189'#0'190'#0'191'#0'192'#0'193'#0'194'#0'195'#0'196'#0'
                      

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab数字图像处理 入门基础发布时间:2022-07-18
下一篇:
支撑向量机 SVM 学习笔记一 (Matlab代码)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap