{"id":1217,"date":"2014-09-11T15:21:44","date_gmt":"2014-09-11T15:21:44","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1217"},"modified":"2014-09-11T15:21:44","modified_gmt":"2014-09-11T15:21:44","slug":"%e7%94%a8python%e7%bb%9f%e8%ae%a1%e6%96%87%e4%bb%b6%e8%a1%8c%e6%95%b0%e7%9a%84%e5%87%a0%e4%b8%aa%e6%96%b9%e6%b3%95%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1217.html","title":{"rendered":"\u7528Python\u7edf\u8ba1\u6587\u4ef6\u884c\u6570\u7684\u51e0\u4e2a\u65b9\u6cd5\u603b\u7ed3"},"content":{"rendered":"<p>\u4e4b\u524d\u5176\u5b9e\u78b0\u5230\u8fc7\u5f88\u591a\u6b21\u4e86\uff0c\u4e5f\u5927\u6982\u77e5\u9053\u4e00\u4e9b\uff0c\u4f46\u4e00\u76f4\u90fd\u6ca1\u6709\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u4ee5\u8fd9\u6b21\u5728\u641c\u7d22\u4e86\u5f88\u591a\u65b9\u6cd5\u4e4b\u540e\u8fdb\u884c\u4e2a\u5927\u4f53\u7684\u603b\u7ed3\uff0c\u65b9\u4fbf\u4ee5\u540e\u67e5\u9605\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#!\/usr\/bin\/env python\n# -*- coding: utf-8 -*-\nimport time, os, sys\nfname=\"C:\/Python27\/NEWS.txt\"\n#fname=sys.argv[1].strip()\n\ndef timeo(fun, n=10):\n    start = time.clock()\n    for i in xrange(n): fun()\n    end = time.clock()\n    thetime = end-start\n    return fun.__name__, thetime                        #\u8fd4\u56de\u51fd\u6570\u540d\u79f0\uff0c\u51fd\u6570\u8fd0\u884c10\u6b21\u603b\u65f6\u95f4\u7684 \u5143\u7ec4\n\ndef linecount_wc():\n    return int(os.popen('wc -l nuc').read().split()[0]) #\u4f7f\u7528\u5916\u90e8\u7cfb\u7edf\u547d\u4ee4 wc -l\n\ndef linecount_1( ): #\u4f7f\u7528\u65b9\u6cd51\n    return len(open(fname).readlines())\n\ndef linecount_2():  #\u4f7f\u7528\u65b9\u6cd52\n    count = 0\n    for line in open(fname).xreadlines(): count += 1\n    return count\n\ndef linecount_3():  #\u4f7f\u7528\u65b9\u6cd53\n    count = 0\n    thefile = open(fname)\n    while 1:\n        buffer = thefile.read(65536)\n        if not buffer: break\n        count += buffer.count('n')\n    return count\n\ndef linecount_4():  #\u6dfb\u52a0\u7684\u65b9\u6cd54\n    count=0\n    for line in open(fname):\n        count+=1\n    return count\n\nfor f in linecount_1, linecount_2, linecount_3, linecount_4:\n    print f.__name__, f()\n\nfor f in linecount_1, linecount_2, linecount_3, linecount_4:\n    print \"%s: %.2f\" % timeo(f)\n\n'''\nlinecount_1 4929090\nlinecount_2 4929090\nlinecount_3 4929090\nlinecount_4 4929090\nlinecount_1: 17.92\nlinecount_2: 18.14\nlinecount_3: 9.84\nlinecount_4: 18.42\n\u4f46\u6211\u5728EditPlus\u548cNotepad++\u4e2d\u6253\u5f00\u65f6\uff0c\u663e\u793a\u7684\u884c\u6570\u5206\u522b\u4e3a\uff1a4929060 \/ 4929090 \u4e3a\u4ec0\u4e48\u4f1a\u6709\u70b9\u5dee\u8ddd\u2026\u2026\n'''<\/pre>\n<p>\u8d77\u6e90\u4e8e\u5544\u6728\u9e1f\u793e\u533a\u7684\u4e00\u4e2a\u8ba8\u8bba\uff1a<a href=\"http:\/\/wiki.woodpecker.org.cn\/moin\/PyCkBk-4-7\" target=\"_blank\">http:\/\/wiki.woodpecker.org.cn\/moin\/PyCkBk-4-7<\/a>\uff0c\u7136\u540e\u6211\u662f\u5728\u201c<a href=\"http:\/\/hi.baidu.com\/javalang\/item\/cd94331ce9702e0ce75c3671\" target=\"_blank\">Python\uff1a\u7edf\u8ba1\u6587\u4ef6\u884c\u6570<\/a>\u201d\u4e2d\u627e\u5230\u7684\u8ba1\u65f6\u4ee3\u7801\uff0c\u4e0d\u8fc7\u73b0\u5728Python\u6709timeit\u6a21\u5757\u4e86\uff0c\u6240\u4ee5\u8fd9\u4e2a\u5c31\u81ea\u5df1\u770b\u7740\u529e\u4e86\uff0c\u600e\u4e48\u65b9\u4fbf\/\u987a\u624b\u600e\u4e48\u6765\u5c31\u597d\u4e86\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u603b\u7ed3<\/strong><\/span>\uff1a\u5728\u5185\u5b58\u4e0d\u662f\u592a\u591a\u7684\u60c5\u51b5\u4e0b\uff0c\u4e00\u884c\u4e00\u884c\u7684\u8bfb\u53d6\u7136\u540e\u8ba1\u6570\u5176\u5b9e\u662f\u4e00\u4e2a\u6700\u5feb\u7684\u65b9\u6cd5\u3002\u7b28\u65b9\u6cd5\u4e0d\u610f\u5473\u7740\u4f4e\u6548\uff01\u53ea\u8981\u80fd\u8fbe\u5230\u76ee\u7684\u5c31\u884c\u3002<\/p>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# coding=utf-8\n\n# method.1\ncount = len(open(filepath,'rU').readlines())\ncount2 = len(linecache.getlines(filepath))\n\n# method.2\ncount = -1\nfor count, line in enumerate(open(filepath, 'rU')):\n\tpass\ncount += 1\n\n#method.3\ncount = 0\nthefile = open(filepath, 'rb')\nwhile True:\n\tbuffer = thefile.read(8192*1024)\n\tif not buffer:\n\t\tbreak\n\tcount += buffer.count('n')\nthefile.close()\n\n#method.4\ndef linecount_w( ):\n    return int(os.popen('wc -l \"C:Python27NEWS.txt\"').read().split()[0])\n<\/pre>\n<p>&nbsp;<\/p>\n<h6>\u4e00\u4e9b\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/gauravpandey.com\/wordpress\/?p=507\" target=\"_blank\">read(), readline(), readlines() &amp; xreadlines() func in Python<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/lingedeng\/article\/details\/7106661\" target=\"_blank\">Python\u2014\u2014\u6587\u4ef6\u5904\u7406<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5176\u5b9e\u78b0\u5230\u8fc7\u5f88\u591a\u6b21\u4e86\uff0c\u4e5f\u5927\u6982\u77e5\u9053\u4e00\u4e9b\uff0c\u4f46\u4e00\u76f4\u90fd\u6ca1\u6709\u8fdb\u884c\u603b\u7ed3\uff0c\u6240\u4ee5\u8fd9\u6b21\u5728\u641c\u7d22\u4e86\u5f88\u591a\u65b9\u6cd5\u4e4b\u540e\u8fdb\u884c\u4e2a\u5927\u4f53\u7684\u603b\u7ed3\uff0c\u65b9 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[8],"class_list":["post-1217","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python"],"views":2693,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=1217"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1217\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}