OS: Red Hat Enterprise Linux AS release 4
Mem: 32G
CPU: 4核2.4GHz——Intel(R) Xeon(R) E5645
--more--
(1)文件读取效率:读每行37个字符的1000000行(1e6)的文件,======================================
方法一: for line in open(): all+=len(line)
方法二: for line in open().readlines(): all+=len(line)
方法三: for line in open().xreadlines(): all+=len(line)
方法四: while True: line=f.readline(); if not line: break; all+=len(line)
每种方法各跑5遍,反序再跑5遍,时间如下:
2.24872994423
4.20436000824
2.2626888752
6.20705699921
----------------------------------------------------------------------
6.20805597305
2.25927710533
4.11336898804
2.23059678078
说明 readline 最慢, 什么都不加 相当于 用了 xreadlines 最快,说明 iter优化 还是有效果的。
算了一下xreadlines大概每秒读 100MB 的内容,一行一行读变成30MB,这时才体现出python的低效。
(2)08 和 09===============================================================
千万别写这句话: a=08
(3)global==================================================================
测了一下,貌似是这样的感觉:
① 定义的时候 global a; a=xxx; 表示全局变量,给别的地方提供访问权限。
② 另一个位置调用的时候,print a 是可以的,但是 print a; a=yyy就报错,用 global a; a=yyy; 就可以了,不知道具体怎么实现的。
③ 上一条中如果是可改元素(lis[0]="another")没问题,不可改元素改变(str="another")会导致 UnboundLocalError: local variable 'x' referenced before assignment
python手册说得好:“任何在函数体内的赋值将会使其成为一个本地变量名。”(怎么看都觉得挺sx的)
不管怎么说这个设计都相当奇葩,附代码:
>>> X=11
>>> def f(): print X
...
>>> f()
11
>>> def g(): X=22; print X
...
>>> g()
22
>>> def gg(): print X; X=22; print X
...
>>> gg()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 1, in gg
UnboundLocalError: local variable 'X' referenced before assignment
>>>
(4)垃圾回收=================================================================
利用 class 的 __del__ 默认析构函数观察 gc 机制:
>>>class C(object):
... def __init__(self):
... self.val=1
... def __del__(self):
... print "delete",self.val
...
>>>c=C()
>>>c
<__main__.C object at 0x2a955d7650>
>>>e=c
>>>e
<__main__.C object at 0x2a955d7650>
>>>e=2
>>>c=2
>>>e=0
>>>e
delete 1
0
>>>
由此可知有个延迟删除的机制,c=2后就没有 C 的实例了,但是还不调用析构。
但如果是这样,则回收很及时:
>>>c=C()
>>>e=c
>>>c=4
>>>e=3
delete 1
>>>
多次测试表明只要代码散乱,回收就可能不及时。
(5)防止程序崩溃=================================================================
这个方法太好了,我是怎么想出来的?
while True:
try:
my_work()
except:
print "Error ..."
# zombie program continues~
time.sleep(...)
ORZ.........................
(6)split 参数留空的特殊作用!================================
- str.split([sep[, maxsplit]])
-
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). Ifmaxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
2013年2月27日 22:28
我不觉得 global 声明的设计哪里奇葩了。global x 语句告诉 Python 编译器,你这里的 x 是全局环境中的 x 而非局部的 x。于是你就不能在局部作用域里创建相同的名字了。Python 的名字使用前不需要声明,所以只能这样子了。
2013年2月28日 00:16
@依云: 嗯,设计的挺好,只是我不太会用~感谢大神指点...orz
2023年1月21日 20:30
Updating Python on Red Hat Enterprise Linux AS release 4 with 32GB of RAM and a Quad Core 2.4GHz Intel(R) Xeon(R) E5645 CPU can <a href="https://mikemoorerealtor.com/properties/sold/">real estate property South Port</a> significantly improve the efficiency of file reading. We tested it by reading a file with 1 million lines of 37 characters each written in English (US) and found a remarkable improvement.
2023年1月21日 20:31
Updating Python on Red Hat Enterprise Linux AS release 4 with 32GB of RAM and a Quad Core 2.4GHz Intel(R) Xeon(R) E5645 CPU can significantly real estate property South Port improve the efficiency of file reading. We tested it by reading a file with 1 million lines of 37 characters each written in English (US) and found a remarkable improvement.