Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Monday, April 7, 2008

用psyco优化python程序

psyco是python的一个扩展,它通过编译python代码来提升python程序的速度。

使用的方法很简单,在python程序里加上下面两句就可以了:

import psyco
psyco.full()

psyco会在程序运行时编译部分代码(具体怎么编译还不清楚),因此程序的初始化时间会增加,但运行速度会变快。

这里有一篇gentoo-wiki介绍用psyco优化portage速度的:

http://gentoo-wiki.com/TIP_Speed_up_portage_with_Psyco

这篇文章中说,用了psyco以后,portage性能可以提升2-5倍,不过同时也警告说psyco现在还不稳定,可能会带来很多问题,因此不推荐使用。

我试了一下,用了psyco的emerge速度快多了(看来2-5倍不是吹的),有一些不影响使用的小问题,比如没有用sudo的emerge会抛异常、确认删除时选“no”也会抛异常。我猜测这都是因为psyco对try-except语句支持得不好,原来的程序里catch住的异常现在引发了psyco的新异常。

不过这些问题都不影响正常使用。继续观望。

另外,有些别的python程序原生支持psyco,像Frets On Fire,我看了它的源码,它会初试使用psyco。我装了psyco以后再玩Frets On Fire,感觉快了一点,不过没有portage那么明显。

...
Read More

Sunday, March 30, 2008

用Python删除Eclipse的旧插件

Eclipse升级插件以后似乎没有办法删除旧版本的插件。

于是我写了个python脚本清除旧插件。

import os
import glob

def removeOldFiles(dir):
  os.chdir(dir)
  files = glob.glob('*')
  files.sort()
  prevfile = files[0]
  for filename in files[1:]:
    if (filename.find(prevfile.partition('_')[0]) == 0):
      os.system('rm -rf ' + prevfile)
    prevfile = filename

# main
removeOldFiles('/usr/lib/eclipse-3.3/plugins')
removeOldFiles('/usr/lib/eclipse-3.3/features')


...
Read More
 
/* google analytics */