Python example: possible speedup?

Carel Fellinger cfelling at iae.nl
Fri Sep 10 16:29:10 EDT 1999


Hrvoje Niksic <hniksic at srce.hr> wrote:
> As a Python exercise, I wrote a simple program to "scratch an itch",
> i.e. do something useful.  However, I found that Python's lack of
> speed really bytes me here, so I'd like to hear suggestions for
> speedup.  People who don't like that kind of topic, please skip to the 
> following article.  Others, read on.

although many have answered you already, I can't resist in posting yet
another one. Your code takes almost 7 secs on my machine, mine takes a
little over 1 sec, and it's still OO and generic. Besides, I didn't use
any fance Python tricks, just the combination of lasy evaluation and bulk
IO did the trick here.

-----------------
#!/usr/bin/python

import string

class Dpkg_Munger:

    def munge_all(self, file):
	for raw_package in string.split(open(file).read(), '\n\n'):
	    if raw_package!='':
		self.raw_package = '\n' + raw_package + '\n'
		self.munge_one(self)

    def __getitem__(self, key, find=string.find):
	i = find(self.raw_package, '\n'+key+':')
	if i == -1:
	    raise IndexError
	i = i + len(key) + 3
	if self.raw_package[i]==' ':
	    i = i + 1
	j = i - 1
	while 1:
	    j = find(self.raw_package,'\n',j+1)
	    if self.raw_package[j+1] != ' ' and self.raw_package[j+1] != '\t':
		break
	return self.raw_package[i:j]


class process_status(Dpkg_Munger):

    def __init__(self, file):
	self.installed = {}
	self.munge_all(file)

    def munge_one(self, package):
	name = package['Package']
	status = package['Status']
	if string.split(status, ' ')[2] == 'installed':
	    self.installed[name] = 1


class process_available(Dpkg_Munger):

    def __init__(self, file, installed):
	self.installed, self.sizes = installed, {}
	self.munge_all(file)

    def munge_one(self, package):
	name = package['Package']
	size = package['Installed-Size']
	if self.installed.has_key(name):
	    self.sizes[name] = string.atoi(size)


def main():
    installed = process_status('/var/lib/dpkg/status').installed
    sizes = process_available('/var/lib/dpkg/available', installed).sizes
    lst = sizes.keys()
    lst.sort(lambda a, b, sizes=sizes: cmp(sizes[b], sizes[a]))
    for pack in lst:
        print "%s: %d" % (pack, sizes[pack])

if __name__ == '__main__':
    main()

-- 
groetjes, carel




More information about the Python-list mailing list