对局部常数的表现? [英] Performance on local constants?

查看:55
本文介绍了对局部常数的表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是Python的新手(一个多月一点)。我想知道 - 这是这样的事情:


s = re.compile(''what'')


def t(whatnot):

返回s.search(whatnot)

$ x $ b for x in xrange(1000):

打印t(某些东西[i])


明显快于这样的东西:


def t(whatnot):

s = re.compile(''what'')

返回s.search(whatnot)

$ x $ b for i in xrange(1000) :

结果= t(某事[i])


?或者Python是否足够聪明,看到每次调用时s的值都是相同的

,因此只编译一次?


-

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 - 传递它

解决方案

开12月22日上午10点53分,William McBrine< wmcbr ... @ users.sf.netwrote:


大家好,


我是Python的新手(一个月多一点)。我想知道 - 这是这样的事情:


s = re.compile(''what'')


def t(whatnot):

返回s.search(whatnot)

$ x $ b for x in xrange(1000):

打印t(某些东西[i])


明显快于这样的东西:


def t(whatnot):

s = re.compile(''what'')

返回s.search(whatnot)

$ x $ b for i in xrange(1000) :

结果= t(某事[i])


?或者Python是否足够聪明,看到每次调用时s的值都是相同的

,因此只编译一次?


-

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 - 传递给



Python RE'做有一个缓存,但告诉它多次编译

需要时间。


最好做的文档说并编译你的RE''一旦使用之前,你可以使用




timeit模块: http://www.diveintopython.org/perfor...ng/timeit.html

将允许您执行自己的时间。


- Paddy。


12月22日晚上9:53,William McBrine< wmcbr ... @ users.sf.netwrote:


大家好,


我是Python新手(稍微超过一点)一个月)。我想知道 - 这是这样的事情:


s = re.compile(''what'')


def t(whatnot):

返回s.search(whatnot)

$ x $ b for x in xrange(1000):

打印t(某些东西[i])


明显快于这样的东西:


def t(whatnot):

s = re.compile(''what'')

返回s.search(whatnot)

$ x $ b for i in xrange(1000) :

结果= t(某事[i])





No.


或者是Python聪明到足以看到s的值将是相同的
每次通话时





否。它没有水晶球。


因此只编译一次?



但它足够聪明,可以维护一个缓存,从而达到所需的

结果。


为什么你不做一些时间?


当你在它的时候,试试这个:


def t2(什么):

返回re.search(''what'',诸如此类)


和这个:


t3 = re.compile(''what'')。搜索


HTH,

John


William McBrine< wm ****** @ users.sf.netwrote:


大家好,


我是Python的新手(一个多月一点)。我想知道 - 这是这样的事情:


s = re.compile(''what'')


def t(whatnot):

返回s.search(whatnot)

$ x $ b for x in xrange(1000):

打印t(某些东西[i])


明显快于这样的东西:


def t(whatnot):

s = re.compile(''what'')

返回s.search(whatnot)

$ x $ b for i in xrange(1000) :

结果= t(某事[i])


?或者Python是否足够聪明,每次调用时s的值都是相同的

,因此只编译一次?



回答这些问题的最佳方法是自己尝试使用

。看一下库中的''timeit.py'':你可以运行

它作为脚本来计算简单的东西或从更长的脚本中导入它。


C:\Python25> python lib / timeit.py -s" import re; s = re.compile(''whatnot'')" " s.search(''一些包含whatnot''的长字符串)"

1000000循环,最佳3:每循环使用1.05


C:\Python25> python lib / timeit.py -s" import re" re.compile(''whatnot'')。search(''一些包含whatnot''的长字符串)"

100000循环,最佳3:3.76 usec per loop


C:\Python25> python lib / timeit.py -s" import re" re.search(''whatnot'',''一些长字符串包含什么'')"

100000循环,最佳3:3.98 usec每循环


因此,如果你没有预先编译正则表达式,那么它看起来需要几微秒的开销。如果你有如上所述的简单比赛,这可能很重要

,如果匹配是

复杂且缓慢,则无关紧要。


您也可以单独测试编译时间:


C:\Python25> python lib / timeit.py -s" import re" re.compile(''whatnot'')"

100000循环,最佳3:2.36每循环使用


C:\Python25> ; python lib / timeit.py -s" import re" " re.compile( '' <??(?:P | DIV)[^>] *>(P< pat0>(:( P< atag0> \\<一个[^> ;] * \\>)\\< IMG [^>] + class\\s * = [^ =>] *标题[^>] +&\\ GT ; \\< / a\\>)|&\\ LT; IMG [^>] + class\\s * = [^ =>] *标题[^> ] + \\>)< /(?: P |格)> |?(P< PAT1>(:( P< atag1> \\<一个[^>] *? \\>)\\< IMG [^>] + class\\s * = [^ =>] *标题[^>] +&\\ GT; \ \< / a\\>)|&\\ LT; IMG [^>] + class\\s * = [^ =>] *标题[^>] + \\ \\\>)'')"

100000循环,最佳3:2.34每循环usec


无论你使用的是什么都没有区别琐碎的正则表达式

或复杂的表达式:Python记得(如果我没记错的话)它编译的最后一个
100表达式,所以编译开销会非常好

常数。

Hi all,

I''m pretty new to Python (a little over a month). I was wondering -- is
something like this:

s = re.compile(''whatever'')

def t(whatnot):
return s.search(whatnot)

for i in xrange(1000):
print t(something[i])

significantly faster than something like this:

def t(whatnot):
s = re.compile(''whatever'')
return s.search(whatnot)

for i in xrange(1000):
result = t(something[i])

? Or is Python clever enough to see that the value of s will be the same
on every call, and thus only compile it once?

--
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on

解决方案

On Dec 22, 10:53 am, William McBrine <wmcbr...@users.sf.netwrote:

Hi all,

I''m pretty new to Python (a little over a month). I was wondering -- is
something like this:

s = re.compile(''whatever'')

def t(whatnot):
return s.search(whatnot)

for i in xrange(1000):
print t(something[i])

significantly faster than something like this:

def t(whatnot):
s = re.compile(''whatever'')
return s.search(whatnot)

for i in xrange(1000):
result = t(something[i])

? Or is Python clever enough to see that the value of s will be the same
on every call, and thus only compile it once?

--
09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 -- pass it on

Python RE''s do have a cache but telling it to compile multiple times
is going to take time.

Best to do as the docs say and compile your RE''s once before use if
you can.

The timeit module: http://www.diveintopython.org/perfor...ng/timeit.html
will allow you to do your own timings.

- Paddy.


On Dec 22, 9:53 pm, William McBrine <wmcbr...@users.sf.netwrote:

Hi all,

I''m pretty new to Python (a little over a month). I was wondering -- is
something like this:

s = re.compile(''whatever'')

def t(whatnot):
return s.search(whatnot)

for i in xrange(1000):
print t(something[i])

significantly faster than something like this:

def t(whatnot):
s = re.compile(''whatever'')
return s.search(whatnot)

for i in xrange(1000):
result = t(something[i])

?

No.

Or is Python clever enough to see that the value of s will be the same

on every call,

No. It doesn''t have a crystal ball.

and thus only compile it once?

But it is smart enough to maintain a cache, which achieves the desired
result.

Why don''t you do some timings?

While you''re at it, try this:

def t2(whatnot):
return re.search(''whatever'', whatnot)

and this:

t3 = re.compile(''whatever'').search

HTH,
John


William McBrine <wm******@users.sf.netwrote:

Hi all,

I''m pretty new to Python (a little over a month). I was wondering -- is
something like this:

s = re.compile(''whatever'')

def t(whatnot):
return s.search(whatnot)

for i in xrange(1000):
print t(something[i])

significantly faster than something like this:

def t(whatnot):
s = re.compile(''whatever'')
return s.search(whatnot)

for i in xrange(1000):
result = t(something[i])

? Or is Python clever enough to see that the value of s will be the same
on every call, and thus only compile it once?

The best way to answer these questions is always to try it out for
yourself. Have a look at ''timeit.py'' in the library: you can run
it as a script to time simple things or import it from longer scripts.

C:\Python25>python lib/timeit.py -s "import re;s=re.compile(''whatnot'')" "s.search(''some long string containing a whatnot'')"
1000000 loops, best of 3: 1.05 usec per loop

C:\Python25>python lib/timeit.py -s "import re" "re.compile(''whatnot'').search(''some long string containing a whatnot'')"
100000 loops, best of 3: 3.76 usec per loop

C:\Python25>python lib/timeit.py -s "import re" "re.search(''whatnot'', ''some long string containing a whatnot'')"
100000 loops, best of 3: 3.98 usec per loop

So it looks like it takes a couple of microseconds overhead if you
don''t pre-compile the regular expression. That could be significant
if you have simple matches as above, or irrelevant if the match is
complex and slow.

You can also try measuring the compile time separately:

C:\Python25>python lib/timeit.py -s "import re" "re.compile(''whatnot'')"
100000 loops, best of 3: 2.36 usec per loop

C:\Python25>python lib/timeit.py -s "import re" "re.compile(''<(?:p|div)[^>]*>(?P<pat0>(?:(?P<atag0>\\<a[^>]*\\>)\\<img[^>]+class\\s*=[^=>]*captioned[^>]+\\>\\</a\\>)|\\<img[^>]+class\\s*=[^=>]*captioned[^>]+\\>)</(?:p|div)>|(?P<pat1>(?:(?P<atag1>\\<a[^>]*\\>)\\<img[^>]+class\\s*=[^=>]*captioned[^>]+\\>\\</a\\>)|\\<img[^>]+class\\s*=[^=>]*captioned[^>]+\\>)'')"
100000 loops, best of 3: 2.34 usec per loop

It makes no difference whether you use a trivial regular expression
or a complex one: Python remembers (if I remember correctly) the last
100 expressions it compiled,so the compilation overhead will be pretty
constant.


这篇关于对局部常数的表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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