Ram*_*rar 2 python optimization
f = open('wl4.txt', 'w')
hh = 0
######################################
for n in range(1,5):
for l in range(33,127):
if n==1:
b = chr(l) + '\n'
f.write(b)
hh += 1
elif n==2:
for s0 in range(33, 127):
b = chr(l) + chr(s0) + '\n'
f.write(b)
hh += 1
elif n==3:
for s0 in range(33, 127):
for s1 in range(33, 127):
b = chr(l) + chr(s0) + chr(s1) + '\n'
f.write(b)
hh += 1
elif n==4:
for s0 in range(33, 127):
for s1 in range(33, 127):
for s2 in range(33,127):
b = chr(l) + chr(s0) + chr(s1) + chr(s2) + '\n'
f.write(b)
hh += 1
######################################
print "We Made %d Words." %(hh)
######################################
f.close()
Run Code Online (Sandbox Code Playgroud)
那么,有没有什么方法可以让它更快?
可以进一步显着改进.
以下脚本文件演示了这些,仅使用(简洁)4号循环(占用了90%以上的时间).
方法0:OP的原始代码
方法1:John Kugleman的解决方案
方法2:(1)并将一些字符串连接移出内部循环
方法3:(2)并将代码放在函数中 - 访问局部变量比全局变量快得多.任何脚本都可以这样做.许多脚本应该这样做.
方法4:(3)并在列表中累积字符串然后加入它们并写入它们.请注意,这会使用您可能不相信的内存.我的代码不会尝试为整个文件执行此操作,因为(127 - 33)**4是78M字符串.在一个32位的盒子上,单独列表为78*4 = 312Mb(忽略列表末尾未使用的内存),加上str*对象的78*28 = 2184 Mb(sys.getsizeof("1234")产生) 28),加上78*5 = 390 Mb的连接结果.你只是吹了你的用户地址空间或你的ulimit或其他东西吹.或者,如果您有1 Gb的实际内存,其中128Mb已被视频驱动程序snarfed,但有足够的交换空间,您有时间吃午餐(如果运行特定的操作系统,晚餐也是如此).
方法5:(4)并且不要在列表中询问其追加属性的下落7800万次:-)
这是脚本文件:
import time, sys
time_function = time.clock # Windows; time.time may be better on *x
ubound, which = map(int, sys.argv[1:3])
t0 = time_function()
if which == 0:
### original ###
f = open('wl4.txt', 'w')
hh = 0
n = 4
for l in range(33, ubound):
if n == 1:
pass
elif n == 2:
pass
elif n == 3:
pass
elif n == 4:
for s0 in range(33, ubound):
for s1 in range(33, ubound):
for s2 in range(33,ubound):
b = chr(l) + chr(s0) + chr(s1) + chr(s2) + '\n'
f.write(b)
hh += 1
f.close()
elif which == 1:
### John Kugleman ###
f = open('wl4.txt', 'w')
chars = [chr(c) for c in range(33, ubound)]
hh = 0
for l in chars:
for s0 in chars:
for s1 in chars:
for s2 in chars:
b = l + s0 + s1 + s2 + '\n'
f.write(b)
hh += 1
f.close()
elif which == 2:
### JohnK, saving + ###
f = open('wl4.txt', 'w')
chars = [chr(c) for c in range(33, ubound)]
hh = 0
for L in chars: # "L" as in "Legible" ;-)
for s0 in chars:
b0 = L + s0
for s1 in chars:
b1 = b0 + s1
for s2 in chars:
b = b1 + s2 + '\n'
f.write(b)
hh += 1
f.close()
elif which == 3:
### JohnK, saving +, function ###
def which3func():
f = open('wl4.txt', 'w')
chars = [chr(c) for c in range(33, ubound)]
nwords = 0
for L in chars:
for s0 in chars:
b0 = L + s0
for s1 in chars:
b1 = b0 + s1
for s2 in chars:
b = b1 + s2 + '\n'
f.write(b)
nwords += 1
f.close()
return nwords
hh = which3func()
elif which == 4:
### JohnK, saving +, function, linesep.join() ###
def which4func():
f = open('wl4.txt', 'w')
chars = [chr(c) for c in range(33, ubound)]
nwords = 0
for L in chars:
accum = []
for s0 in chars:
b0 = L + s0
for s1 in chars:
b1 = b0 + s1
for s2 in chars:
accum.append(b1 + s2)
nwords += len(accum)
accum.append("") # so that we get a final newline
f.write('\n'.join(accum))
f.close()
return nwords
hh = which4func()
elif which == 5:
### JohnK, saving +, function, linesep.join(), avoid method lookup in loop ###
def which5func():
f = open('wl4.txt', 'w')
chars = [chr(c) for c in range(33, ubound)]
nwords = 0
for L in chars:
accum = []; accum_append = accum.append
for s0 in chars:
b0 = L + s0
for s1 in chars:
b1 = b0 + s1
for s2 in chars:
accum_append(b1 + s2)
nwords += len(accum)
accum_append("") # so that we get a final newline
f.write('\n'.join(accum))
f.close()
return nwords
hh = which5func()
else:
print "Bzzzzzzt!!!"
t1 = time_function()
print "Method %d made %d words in %.1f seconds" % (which, hh, t1 - t0)
Run Code Online (Sandbox Code Playgroud)
以下是一些结果:
C:\junk\so>for %w in (0 1 2 3 4 5) do \python26\python wl4.py 127 %w
C:\junk\so>\python26\python wl4.py 127 0
Method 0 made 78074896 words in 352.3 seconds
C:\junk\so>\python26\python wl4.py 127 1
Method 1 made 78074896 words in 183.9 seconds
C:\junk\so>\python26\python wl4.py 127 2
Method 2 made 78074896 words in 157.9 seconds
C:\junk\so>\python26\python wl4.py 127 3
Method 3 made 78074896 words in 126.0 seconds
C:\junk\so>\python26\python wl4.py 127 4
Method 4 made 78074896 words in 68.3 seconds
C:\junk\so>\python26\python wl4.py 127 5
Method 5 made 78074896 words in 60.5 seconds
Run Code Online (Sandbox Code Playgroud)
针对OP的问题进行更新
msgstr"""当我尝试添加for循环时,我收到了accum_append的内存错误..问题是什么?"""
我不知道问题是什么; 我不能在这个距离读你的代码.猜猜:如果你想做长度== 5,你可能已经accum在错误的地方进行了初始化和写入,并且accum试图超越系统内存的容量(正如我希望我之前已经解释过的那样).
msgstr"""现在方法5是最快的一个,但是它可以说出长度4 ...我怎么能做出我想要的多少?:)"""
您有两种选择:(1)您继续使用嵌套for循环(2),查看不使用嵌套for循环的答案,并动态指定长度.
方法4和5通过使用获得了加速,accum但是这样做的方式是根据对将使用多少内存的确切知识而定制的.
以下是另外3种方法.101是tgray的方法,没有额外的内存使用.201是Paul Hankin的方法(加上一些写文件代码),同样没有额外的内存使用.这两种方法速度大致相同,并且在方法3的速度范围内.它们都允许动态指定所需的长度.
方法102是使用固定的1Mb缓冲区的tgray方法 - 它试图通过减少对f.write()的调用次数来节省时间......您可能希望尝试使用缓冲区大小.如果您愿意,可以创建正交202方法.请注意,tgray的方法itertools.product用于您需要Python 2.6,而Paul Hankin的方法使用已经存在一段时间的生成器表达式.
elif which == 101:
### tgray, memory-lite version
def which101func():
f = open('wl4.txt', 'w')
f_write = f.write
nwords = 0
chars = map(chr, xrange(33, ubound)) # create a list of characters
length = 4 #### length is a variable
for x in product(chars, repeat=length):
f_write(''.join(x) + '\n')
nwords += 1
f.close()
return nwords
hh = which101func()
elif which == 102:
### tgray, memory-lite version, buffered
def which102func():
f = open('wl4.txt', 'w')
f_write = f.write
nwords = 0
chars = map(chr, xrange(33, ubound)) # create a list of characters
length = 4 #### length is a variable
buffer_size_bytes = 1024 * 1024
buffer_size_words = buffer_size_bytes // (length + 1)
words_in_buffer = 0
buffer = []; buffer_append = buffer.append
for x in product(chars, repeat=length):
words_in_buffer += 1
buffer_append(''.join(x) + '\n')
if words_in_buffer >= buffer_size_words:
f_write(''.join(buffer))
nwords += words_in_buffer
words_in_buffer = 0
del buffer[:]
if buffer:
f_write(''.join(buffer))
nwords += words_in_buffer
f.close()
return nwords
hh = which102func()
elif which == 201:
### Paul Hankin (needed output-to-file code added)
def AllWords(n, CHARS=[chr(i) for i in xrange(33, ubound)]):
#### n is the required word length
if n == 1: return CHARS
return (w + c for w in AllWords(n - 1) for c in CHARS)
def which201func():
f = open('wl4.txt', 'w')
f_write = f.write
nwords = 0
for w in AllWords(4):
f_write(w + '\n')
nwords += 1
f.close()
return nwords
hh = which201func()
Run Code Online (Sandbox Code Playgroud)
您可以创建range(33, 127)一次并将其保存.不必重复创建它会在我的机器上将运行时间缩短一半.
chars = [chr(c) for c in range(33, 127)]
...
for s0 in chars:
for s1 in chars:
for s2 in chars:
b = l + s0 + s1 + s2 + '\n'
f.write(b)
hh += 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1153 次 |
| 最近记录: |