| | 45 | |
| | 46 | == hidden read-ahead buffer == |
| | 47 | * [http://docs.python.org/library/stdtypes.html#file.next Built-in Types -- Python v2.6.1 documentation (#file.next)] より |
| | 48 | {{{ |
| | 49 | In order to make a for loop the most efficient way of looping |
| | 50 | over the lines of a file (a very common operation), |
| | 51 | the next() method uses a hidden read-ahead buffer. |
| | 52 | }}} |
| | 53 | |
| | 54 | * next() を見る |
| | 55 | * コード |
| | 56 | {{{ |
| | 57 | #!python |
| | 58 | def printrss(): |
| | 59 | pf = open('/proc/self/status') |
| | 60 | for l in pf: |
| | 61 | if l[:5] == 'VmRSS': |
| | 62 | print l, |
| | 63 | pf.close() |
| | 64 | |
| | 65 | def test(fname='/path/to/huge_file'): |
| | 66 | f = open(fname) |
| | 67 | c = 1 |
| | 68 | for i in f: |
| | 69 | print '===== %d =====' % c |
| | 70 | printrss() |
| | 71 | c += 1 |
| | 72 | f.close() |
| | 73 | |
| | 74 | print 'test1' |
| | 75 | test() |
| | 76 | }}} |
| | 77 | * 結果 |
| | 78 | {{{ |
| | 79 | : : : |
| | 80 | ===== 1 ===== |
| | 81 | VmRSS: 2392 kB |
| | 82 | : : : |
| | 83 | ===== 11212 ===== |
| | 84 | VmRSS: 2408 kB |
| | 85 | }}} |
| | 86 | * ちょっと (16KB) だけ増えてる |
| | 87 | |
| | 88 | * readline() を見る |
| | 89 | * コード |
| | 90 | {{{ |
| | 91 | #!python |
| | 92 | def printrss(): |
| | 93 | pf = open('/proc/self/status') |
| | 94 | for l in pf: |
| | 95 | if l[:5] == 'VmRSS': |
| | 96 | print l, |
| | 97 | pf.close() |
| | 98 | |
| | 99 | def test(fname='/path/to/huge_file'): |
| | 100 | f = open(fname) |
| | 101 | c = 1 |
| | 102 | while True: |
| | 103 | i = f.readline() |
| | 104 | if not i: break |
| | 105 | print '===== %d =====' % c |
| | 106 | printrss() |
| | 107 | c += 1 |
| | 108 | f.close() |
| | 109 | |
| | 110 | print 'test1' |
| | 111 | test() |
| | 112 | }}} |
| | 113 | * 結果 |
| | 114 | {{{ |
| | 115 | : : : |
| | 116 | ===== 1 ===== |
| | 117 | VmRSS: 2396 kB |
| | 118 | : : : |
| | 119 | ===== 11212 ===== |
| | 120 | VmRSS: 2396 kB |
| | 121 | }}} |
| | 122 | * 増えてない |