给同事做了一个小分享,主要是简单介绍一下python这门语言,ppt贴上,期待python能够成为主流!!!
Archives for python
Python简介
unamed
转一个牛人的经验贴
—————
发信人: SlutteryWolf (一生懸命|情色·猥琐·变态·双子狼), 信区: Python
标 题: Tornado的实现还是很强大的
发信站: 水木社区 (Wed Jan 5 13:19:28 2011), 转信
这段时间自己把iostream.py实现了一下,顺便给socket打了个补丁,发现了好几个
有趣的点:
1. socket的C实现中,对于recv函数,直接返回新生成的字符串比传入缓冲区写还要
快。 大概能快10%。代码稍后附上,大家可以自由cProfile (./python -m
cProfile yourcode.py)。
2. Python的速度大大逊于原生代码。
用cProfile做了些调优后的感想。不过大家用Python都是冲着顺手易用去的,所以这
一点倒也无可厚非。
3. Python的List(含string)操作是高度优化过的。
我曾经试图用自己管理的bytearray缓冲区来替代原生的string,因为源代码里面包
括了大量的+=和区段赋值操作,以提高性能。
但是我错了,而且错的很离谱。
现在的bytearray1[x:length] = bytearray2[y:length]操作和我自己写的
memcpy(bytearray2, y, bytearray1, x, length)几乎一样快。
而且如果是反复调用string_a += random_string_b,并不会每次重新分配内存并做
拷贝操作,和预先分配一块内存做搬运基本一样快。
4. 函数调用在Python里面的开销是不小的。对于较为关键的代码,建议手工
inline。当然如果对性能要求再高建议直接C.
5. 用 not not collection_var 来代替 len(collection_var) > 0 的判断语
句。
6. 对于deque,访问deque.popleft,然后根据判断再deque.appendleft,比直接
deque[0]要快。
总之,Tornado的实现考虑到了以上中的大部及重点,如1和3. 它的实现蛮强大,大家
放心使用。。。
———–
post by gmail~
多线程使用pycurl时出现段错误(segmentation fault)
http://www.python-forum.org/pythonforum/viewtopic.php?f=5&t=9395
只需要加一个选项
client.setopt(pycurl.NOSIGNAL, 1)
文档的解释
Pass a long. If it is 1, libcurl will not use any functions that install signal handlers or any functions that cause signals to be sent to the process. This option is mainly here to allow multi-threaded unix applications to still set/use all timeout options etc, without risking getting signals. (Added in 7.10)
If this option is set and libcurl has been built with the standard name resolver, timeouts will not occur while the name resolve takes place. Consider building libcurl with c-ares support to enable asynchronous DNS lookups, which enables nice timeouts for name resolves without signals.