The time in my example case (my name) directly from command line takes actually 156 ms in my computer. Quite a IDLE penalty of redirection. Problem with internal timer also? (time.clock())
With Python 2.6 and psyco 116 ms.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
An example how you should name your variables correct from beginning, I renamed the old variable names to opposite of their meaning (because my full anagram program has parameters in opposite order). The IDLE issue seems to be dismissal performance of multiple small print statements, so I simplified the code to use ', '.join.
Actually this program works from word list from disk and sorts only once, not for each word as some earlier versions. The load takes typically round 50 ms in my computer for the 57047 word UK dictionary.
## Simple sub-anagrams
from time import clock
#{ python specialising compiler
try:
import psyco
psyco.full()
except:
pass
#}
def part_of(bigger, part):
""" goes through the letters of part and returns it
if part is partial anagram of bigger
"""
# each letter of part must be in bigger
for c in part:
if c not in bigger: return ""
bigger=bigger.replace(c, '', 1)
return part
if __name__=="__main__":
t = clock()
wordlist = sorted(open('uk.txt').read().lower().split(), key=len, reverse=True)
t -= clock()
print('Wordlist load and sort took %i ms'%(-t * 1000))
print('''
Generating possible multiword anagram
candidate words in reverse length order.
(To quit enter empty line)''')
while True:
inputword=raw_input('\nGive word: ').lower()
if not inputword:
break
t = clock()
print(', '.join(wd for wd in (part_of(inputword, w)
for w in wordlist)
if wd))
t -= clock()
print('Took %i ms'%(-t * 1000))
print "Bye, bye!"
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
Here is finally one version that is made to run in both Python2 and Python3. It is also possible to translate to C++ with Shedskin 0.7 translator (load/sort time is longer, 188 ms without sort, around 125-140 ms with the sort)
EDIT: Added also possibility to give the name of dictionary file as argument.
## Simple sub-anagrams
from time import clock
import sys
#{ python specialising compiler
try:
import psyco
psyco.full()
except:
pass
#}
try:
input = raw_input
except NameError:
pass
def part_of(bigger, part):
""" goes through the letters of part and returns it
if part is partial anagram of bigger
"""
# each letter of part must be in bigger
for c in part:
if c not in bigger: return ""
bigger=bigger.replace(c, '', 1)
return part
if __name__=="__main__":
t = clock()
wordlist = sorted(open('dict/uk.txt' if len(sys.argv) == 1 else sys.argv[1]).read().lower().split(),
key=len,
reverse=True)
t -= clock()
print('Wordlist load and sort took %i ms'%(-t * 1000))
print('''
Generating possible multiword anagram
candidate words in reverse length order.
(To quit enter empty line)''')
while True:
inputword = input('\nGive word: ').lower()
if not inputword:
break
t = clock()
print(', '.join(wd for wd in (part_of(inputword, w)
for w in wordlist)
if wd))
t -= clock()
print('Took %i ms'%(-t * 1000))
print("Bye, bye!")
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852