Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Python
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Python section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 8th, 2007, 08:13 AM
Authorized User
Join Date: Mar 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Python code optimization

Need some hints in speeding up the Python scripts!!!

(1)Except partial objects module importing, i need to find out how to
improve performance in spatial and time consumption!
%instead of importing a whole gzip module
import gzip
%I use partial import of methods I am going to use, from the gzip module
import compress from gzip
import decompress from gzip

The example is rudimentary, but it explains the problem in hand. Because even the simplest gui aplication extracts 8MB of memory space, and that is absolutely to much...
Please, if any hints come across your mind, feel free to post your code...

Old January 9th, 2009, 04:03 PM
Registered User
Join Date: Jan 2009
Posts: 8
Thanks: 1
Thanked 2 Times in 1 Post
Default The other way around

I'm not sure I understand the question, but I think it's a syntax error.
You are meant to do: from MODULE import something
for example: from gzip import compress
you can also use import gzip.compress

You say that you want to optimise your code by avoiding the import of redundant modules, that's great, I'm not sure how much optimisation you'll get (it depends on how much redundancies you had before) but it sure can't heart. if you want to do some more hardcore optimisation checkout psyco.sourceforge.net
Old December 13th, 2011, 05:13 AM
Registered User
Join Date: Dec 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts


I solved the problem using this (horribly inefficient method):

def createList(word, wordList):
#Made a set, because for some reason permutations was returning duplicates.
#Returns all permutations if they're in the wordList

return set([''.join(item) for item in itertools.permutations(word) if ''.join(item) in wordList])

def main():

a = open('C:\\Python32\\megalist.txt', 'r+')
wordList = [line.strip() for line in a]
maximum = 0
length = 0
maxwords = ""

for words in wordList:
permList = createList(words, wordList)
length = len(permList)
if length > maximum:
maximum = length
maxwords = permList
print (maximum, maxwords)
It took something like 10 minutes to find the five-letter word that has the most dictionary-valid anagrams. I want to run this on words without a letter constraint, but it would take a ludicrous amount of time. Is there anyway to optimize this?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why Python? elvisfeverr Python 6 February 15th, 2019 02:30 AM
python arafat.eajaz Other Programming Languages 0 July 18th, 2008 03:16 AM
RSS Code for python 2.5 (pyxml Vs 4Suite) dsampson BOOK: Beginning XML, 4th Ed ISBN: 978-0-470-11487-2 1 February 12th, 2008 10:49 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.