我可以使用一些帮助,只使用Python代码使这个Python代码运行得更快。 [英] I could use some help making this Python code run faster using only Python code.

查看:71
本文介绍了我可以使用一些帮助,只使用Python代码使这个Python代码运行得更快。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,但是我希望得到一些反馈来自那些了解更多关于Python的人,而不是我现在所知道的。


def scrambleLine (直线):

s =''''

for c in line:

s + = chr(ord(c)| 0x80)

返回s


def descrambleLine(line):

s =''''

for c排队:

s + = chr(ord(c)& 0x7f)

返回s


def scrambleFile(fname, action = 1):

if(path.exists(fname)):

try:

f = open(fname," r" )

toks = fname.split(''。'')

while(len(toks)2):

toks.pop( )

fname =''。''。join(toks)

if(action == 1):

_fname = fname +' '.scrambled''

elif(action == 0):

_fname = fname +''。descrambled''

if(path .exists(_fname)):

os.remove(_fname)

ff = open(_fname," w +" )

if(action == 1):
$ f $ b for f in f:

ff.write(scrambleLine(l))

elif(action == 0):
$ f $ b for f in f:

ff.write(descrambleLine(l))

除了例外,详情:

打印''ERROR ::(%s)''%详细信息

最后:

f.close( )

ff.close()

else:

print''警告::缺少文件"%s" - 无法继续。''%fname

I am new to Python however I would like some feedback from those who
know more about Python than I do at this time.

def scrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) | 0x80)
return s

def descrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) & 0x7f)
return s

def scrambleFile(fname,action=1):
if (path.exists(fname)):
try:
f = open(fname, "r")
toks = fname.split(''.'')
while (len(toks) 2):
toks.pop()
fname = ''.''.join(toks)
if (action == 1):
_fname = fname + ''.scrambled''
elif (action == 0):
_fname = fname + ''.descrambled''
if (path.exists(_fname)):
os.remove(_fname)
ff = open(_fname, "w+")
if (action == 1):
for l in f:
ff.write(scrambleLine(l))
elif (action == 0):
for l in f:
ff.write(descrambleLine(l))
except Exception, details:
print ''ERROR :: (%s)'' % details
finally:
f.close()
ff.close()
else:
print ''WARNING :: Missing file "%s" - cannot continue.'' % fname

推荐答案

9月20日晚上10点59分,Python Maniac< raych ... @ hotmail.comwrote:
On Sep 20, 10:59 pm, Python Maniac <raych...@hotmail.comwrote:

我是Python的新手但是我想从那些知道更多关于Python的人那里获得一些反馈而不是我在这个时候。


def scrambleLine(直线):

s =''''

for c in line:

s + = chr(ord(c)| 0x80)

返回s


def descrambleLine(line):

s =''''

for c in line:

s + = chr(ord(c)& 0x7f)

return s

...
I am new to Python however I would like some feedback from those who
know more about Python than I do at this time.

def scrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) | 0x80)
return s

def descrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) & 0x7f)
return s
...



好​​吧,scrambleLine会删除行尾,所以当你是

解扰

你将立即处理整个文件。这是特别糟糕的

因为你的功能工作方式,一次添加一个角色



s。


可能你最简单的赌注是使用read(N)

对一些小N进行迭代,而不是一次做一行。类似的东西:


process_bytes =(descrambleLine,scrambleLine)[动作]

而1:

r = f.read(16 )

如果不是r:break

ff.write(process_bytes(r))


通常,而不是构建字符串从一个空的

字符串开始并重复添加它,你应该使用''''。join(...)


例如.. 。

def descrambleLine(line):

返回''''。join(chr(ord(c)& 0x7f)for c in the line)


def scrambleLine(line):

返回''''。join(chr(ord(c)| 0x80)for c in line)


它的代码更少,更易读,更快!


-

Paul Hankin

Well, scrambleLine will remove line-endings, so when you''re
descrambling
you''ll be processing the entire file at once. This is particularly bad
because of the way your functions work, adding a character at a time
to
s.

Probably your easiest bet is to iterate over the file using read(N)
for some small N rather than doing a line at a time. Something like:

process_bytes = (descrambleLine, scrambleLine)[action]
while 1:
r = f.read(16)
if not r: break
ff.write(process_bytes(r))

In general, rather than building strings by starting with an empty
string and repeatedly adding to it, you should use ''''.join(...)

For instance...
def descrambleLine(line):
return ''''.join(chr(ord(c) & 0x7f) for c in line)

def scrambleLine(line):
return ''''.join(chr(ord(c) | 0x80) for c in line)

It''s less code, more readable and faster!

--
Paul Hankin


Python Maniac schrieb:
Python Maniac schrieb:

我是Python的新手,但是我希望得到一些反馈来自

比我现在更了解Python。


def sc rambleLine(line):

s =''''

for c in line:

s + = chr(ord(c)| 0x80)

返回s


def descrambleLine(直线):

s =''''

for c in the line:

s + = chr(ord(c)& 0x7f)

返回s
I am new to Python however I would like some feedback from those who
know more about Python than I do at this time.

def scrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) | 0x80)
return s

def descrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) & 0x7f)
return s



这些可能会受益于使用查找字典将ord(c)映射到操作的结果。实际上,它的速度提高了两倍:


导入时间


lt = {}
$ x $ b for x in xrange (255):

lt [chr(i)] = chr(i | 0x80)


def scrambleLine(line):

s =''''

for c in line:

s + = chr(ord(c)| 0x80)

返回s


def scrambleLineLU(直线):

s =''''

for c in line:

s + = lt [c]

返回s

如果__name__ ==" __ main __":

line =" abcdefghijklmnop" * 1000000

start = time.time()

scrambleLine(line)

print time.time() - start


start = time.time()

scrambleLineLU(行)

打印time.time() - 开始


Diez

These might benefit from using a lookup-dictionary that maps ord(c) to
the outcome of the operation. Actually, it becomes twice as fast:

import time

lt = {}
for i in xrange(255):
lt[chr(i)] = chr(i | 0x80)

def scrambleLine(line):
s = ''''
for c in line:
s += chr(ord(c) | 0x80)
return s

def scrambleLineLU(line):
s = ''''
for c in line:
s += lt[c]
return s
if __name__ == "__main__":
line = "abcdefghijklmnop" * 1000000
start = time.time()
scrambleLine(line)
print time.time() - start

start = time.time()
scrambleLineLU(line)
print time.time() - start

Diez


2007年9月20日,Python Maniac< ra ****** @ hotmail.comwrote:
On 9/20/07, Python Maniac <ra******@hotmail.comwrote:

我是Python的新手,但是我想从那些了解更多关于Python的人那里获得一些反馈,而不是我现在所知道的。
I am new to Python however I would like some feedback from those who
know more about Python than I do at this time.



好​​吧,你可以节省一些时间,不要一次申请争夺一行

(如果你不是介意在

扰乱版本中丢失行结尾)。为了使其有效,您可能希望以二进制模式打开

。此外,您的争夺可以使用列表

理解来编写。

Well, you could save some time by not applying the scramble one line
at a time (that is if you don''t mind losing the line endings in the
scrambled version). For that to be effective though, you probably want
to open in binary mode. Also, your scramble can be written using list
comprehension.

展开 | 选择 | Wrap | 行号


这篇关于我可以使用一些帮助,只使用Python代码使这个Python代码运行得更快。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆