{"id":194200,"date":"2020-11-04T02:40:55","date_gmt":"2020-11-03T18:40:55","guid":{"rendered":"http:\/\/4563.org\/?p=194200"},"modified":"2020-11-04T02:40:55","modified_gmt":"2020-11-03T18:40:55","slug":"%e6%b1%82%e4%bc%98%e5%8c%96%e5%8f%af%e8%83%bd","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=194200","title":{"rendered":"\u6c42\u4f18\u5316\u53ef\u80fd"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u4f18\u5316\u53ef\u80fd               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u4e00\u4e2a\u9700\u6c42\u73b0\u5728\u5199\u4e86\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0<br \/>\u4e00\u4e2a functools.reduce \u5b9e\u73b0\uff0c\u80fd\u76f4\u63a5\u8fd4\u56de\u76ee\u6807<br \/>\u4e00\u4e2a itertools.accumulate\uff0c\u8fd4\u56de itertor\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f\u76ee\u6807<\/p>\n<p>timeit \u6d4b\u4e86\u4e00\u4e0b<br \/>reduce \u9700\u65f6\u7ea6 0.01<br \/>accumulate \u8fd4\u56de itertor \u7ea6 0.005 \uff0c\u4f46\u52a0\u4e0a list\uff0c\u53d6[-1]\uff0c\u5c31\u9700\u8981 0.02 \uff0c\u65f6\u95f4\u7ffb\u4e86\u500d<br \/>\u73b0\u5728\u60f3\u77e5\u9053 accumulate \uff08 itertor \uff09\u6709\u6ca1\u6709\u66f4\u5feb\u63d0\u53d6\u6700\u540e\u5143\u7d20\u7684\u65b9\u6cd5\uff1f<br \/>\u8bd5\u8fc7 collections.deque [-1]\uff0c\u53ea\u662f\u7565\u5fae\u6bd4 list \u5feb\u4e00\u70b9\u70b9\uff0c\u7ea6 0.019 \uff0c\u4f5c\u7528\u4e0d\u5927<\/p>\n<p>\u5b9e\u73b0\u7684\u9700\u6c42\uff0c\u5c31\u662f\u4f20\u5165\u4e00\u4e2a functions \u5217\u8868\uff0c\u6216\u8005\u53c2\u6570\u5217\u8868\uff0c\u6c42\u79fb\u52a8\u8ba1\u7b97\u7ed3\u679c\uff0c\u662f\u4e2a\u901a\u7528\u6a21\u5757<br \/>\u9700\u6c42\u662f\u4ec0\u4e48\u4e0d\u662f\u91cd\u70b9\uff0c\u4e3a\u4ec0\u4e48\u6709\u8fd9\u6837\u7684\u9700\u6c42\u4e5f\u4e0d\u662f\u91cd\u70b9\uff0c\u53ea\u95ee itertor \u63d0\u53d6\u4f18\u5316<br \/>\u5c31\u6743\u5f53\u5728\u7814\u7a76 list vs tuple \u90a3\u6837\u65e0\u804a\u5c31\u662f\u4e86      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4204041\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuanbg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ArrayList \u7528 get(i)\u65b9\u6cd5\u6309\u4e0b\u6807\u53d6\u96be\u9053\u4e0d\u662f\u6700\u5feb\u7684\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204042\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u672b\u9879\u76f4\u89c9\u60f3\u5230 deque\uff0c\u4e0d\u597d\u4f7f\u7684\u8bdd\u5927\u6982\u5c31\u662f\u8fed\u4ee3\u5668\u5e95\u5c42\u95ee\u9898\u4e86\u5427\uff0c\u8bd5\u8bd5 cython \u63d2\u4ef6\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204043\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xuanbg <br \/>\u6211\u8bd5\u8bd5<\/p>\n<p>@black11black <br \/>\u6709\u4ec0\u4e48\u6a21\u5757\u63a8\u8350\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204044\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : JeffGe <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             for x in iterable: pass<\/p>\n<p>\u8fd9\u4e2a\u4e5f\u53ef\u4ee5\u53d6\u672b\u9879\u5427                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204045\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : freakxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @imn1 #2 <br \/>@JeffGe #3 <\/p>\n<p>&gt; accumulate \u8fd4\u56de itertor \u7ea6 0.005 \uff0c\u4f46\u52a0\u4e0a list\uff0c\u53d6[-1]\uff0c\u5c31\u9700\u8981 0.02 \uff0c\u65f6\u95f4\u7ffb\u4e86\u500d<\/p>\n<p>*_, last = iterable                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204046\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @freakxx #5<br \/>\u4e0d\u77e5\u9053\u8fd8\u6709\u8fd9\u79cd\u5199\u6cd5\uff0c\u5b66\u4e60\u4e86<\/p>\n<p>timeit 0.013 \uff0c\u7565\u6bd4 reduce \u6162\u4e00\u70b9\u70b9\uff0c\u4f46\u6bd4\u4e4b\u524d\u597d\u5f88\u591a\u4e86<br \/>\u4e0d\u8fc7\u91cd\u70b9\u662f\u5b66\u4e60\u4e86\u8fd9\u79cd\u5feb\u901f\u63d0\u53d6\u7684\u5199\u6cd5\uff0c\u8c22\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204047\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuanbg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5982\u679c\u4f60\u7684\u9700\u6c42\u662f\u53d6\u6700\u540e\u5143\u7d20\uff0c\u4e0d\u59a8\u629b\u5f03\u6570\u7ec4\u6539\u7528\u6808\u3002\u6700\u540e\u4e00\u4e2a\u6c38\u8fdc\u5728\u9876\u90e8\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4204048\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : imn1 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6d4b\u8bd5\u4e86\u51e0\u79cd\u5199\u6cd5<br \/>*_, last = itertools.accumulate(pairs, fun) 0.013<br \/>collections.deque(itertools.accumulate(pairs, fun), 1).pop() 0.013<br \/>next(more_itertools.tail(1, itertools.accumulate(pairs, fun))) 0.017<br \/>collections.deque(itertools.accumulate(pairs, fun))[-1] 0.019<\/p>\n<p>deque \u65f6\u95f4\u4e0d\u7a33\u5b9a\uff0c\u53d8\u5316\u5e45\u5ea6\u5927<\/p>\n<p>#7 \u6c42\u6808\u601d\u8def\u662f\u6700\u5408\u9002\u7684\uff0c\u53ef\u60dc python \u8fed\u4ee3\u7c7b\u578b\u4e0d\u652f\u6301\u53cd\u5411\uff0c\u9700\u8981\u66f4\u9ad8\u7ea7\u5199\u6cd5\uff0c\u65e0\u5948\u80fd\u529b\u4e0d\u591f<\/p>\n<p>more_itertools.nth \u6ca1\u6d4b\u8bd5\uff0c\u5e94\u8be5\u8ddf tail \u5dee\u4e0d\u591a\uff0c\u4e0d\u8fc7\u4ee5\u540e\u4ece\u8fed\u4ee3\u5668\u63d0\u53d6\u4e2d\u95f4\u5143\u7d20\u5e94\u8be5\u5f88\u6709\u7528                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u4f18\u5316\u53ef\u80fd \u8cc7\u6df1\u5927\u4f6c : imn1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194200"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=194200"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/194200\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=194200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=194200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=194200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}