int()比Python 2.3中的long()慢24倍 [英] int() 24 times slower then long() in Python 2.3

查看:100
本文介绍了int()比Python 2.3中的long()慢24倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Python 2.3运行以下代码时:


从时间导入时钟

t1 = clock()

for i in range(10000):a = int(''bbbbaaaa'',16)

t2 = clock()

for i in range(10000):a = long(''bbbbaaaa'',16)

t3 =时钟()

打印(t2-t1)/(t3-t2)


打印出来:23.9673206147

这似乎意味着int转换比

长转换慢24倍。 br />
原因是在Python 2.3中,int转换生成了

警告消息,你*从不*看但消耗了*很多*的CPU。


因此,可能会发生在Python 2.2下表现良好的旧代码突然在$ 2.3下减慢相当大的数量而没有任何令人难以置信的

的变化输出......


Willem Vree


BTW。

您没有看到的消息是:OverflowWarning:string / unicode转换

你可以通过使用

Python -Wall script.py



或包括启动程序来显示它源文本中的以下几行:

导入警告

warnings.resetwarnings()

我认为Python已经过了 - 太专业的工程师

程序员。衰变的第一个迹象?

解决方案

Willem写道:

当我使用Python运行以下代码时2.3:

从时间导入时钟
t1 =时钟()
我在范围内(10000):a = int(''bbbbaaaa'',16)
t2 =时钟()
我在范围内(10000):a = long(''bbbbaaaa'',16)
t3 = clock()
print(t2-t1)/ (t3-t2)

它打印出来:23.9673206147

这似乎意味着int转换比
长转换慢24倍。原因是在Python 2.3中,int转换会生成
警告消息,您永远不会看到这些警告消息但消耗了*很多*的CPU。
我认为Python过于专业的程序员正在过度设计。衰变的第一个迹象?




号我认为warning.py需要一些_more_工程 - 扔进字典

来加速重复电话左右。与此同时:


导入警告

def dummy(* args,** kw):

pass

warnings.warn = dummy


现在看着它加速:)


彼得


2004年7月13日星期二下午3:09,Willem写道:

当我使用Python 2.3运行以下代码时:

从时间导入时钟
t1 =时钟()
我在范围内(10000):a = int(''bbbbaaaa'',16)
t2 =时钟()
我在范围内( 10000):a = long(''bbbbaaaa'',16)
t3 = clock()
print(t2-t1)/(t3-t2)
打印out:23.9673206147

这似乎意味着int转换比
长转换慢24倍。
原因是在Python 2.3中int转换生成< br *>警告消息,你*从不*看到但消耗了*很多*的CPU。

因此可能会发生在Python 2.2下表现良好的旧代码突然
在Python 2.3下放慢了相当大的数量而没有任何令人难以置信的输出变化...




我不认为这段代码确实能够完美地工作在Python2.2下(见下文),

或我错过了什么?


Python 2.2.2(#1,2003年2月24日,19: 13:11)

[GCC 3.2.2 20030222(Red Hat Linux 3.2.2-4)] on linux2

输入help,copyright, "币"或许可证或更多信息。

for i in range(10000):a = int(''bbbbaaaa'',16)
....

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

ValueError:int()文字太大:bbbbaaaa



-

James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net


Willem写道:

因此,在Python 2.2下表现良好的旧代码可能会突然降低相当数量Python 2.3没有任何令人难以置信的输出变化......




IMO一堆无效转换的速度

执行意味着什么都没有。你能想出一个在有意义的背景下显示相同症状的例子

吗?


i。


When I run the follwing code using Python 2.3:

from time import clock
t1 = clock ()
for i in range (10000): a = int (''bbbbaaaa'', 16)
t2 = clock ()
for i in range (10000): a = long (''bbbbaaaa'', 16)
t3 = clock ()
print (t2-t1) / (t3-t2)

it prints out: 23.9673206147

That seems to mean that the int-conversion is 24 times slower than the
long conversion.
The reason is that in Python 2.3 the int-conversion generates
warning messages that you *never* see but that consume a *lot* of CPU.

So it may happen that old code performing well under Python 2.2 suddenly
slows down a considerable amount under Python 2.3 without any percievable
change in the output ...

Willem Vree

BTW.
The message that you don''t see is: OverflowWarning: string/unicode conversion
and you can make it appear by starting the progam with

Python -Wall script.py


or including the following lines in the source text:
import warnings
warnings.resetwarnings()

I suppose that Python is getting over-engineered by too professional
programmers. The first signs of decay?

解决方案

Willem wrote:

When I run the follwing code using Python 2.3:

from time import clock
t1 = clock ()
for i in range (10000): a = int (''bbbbaaaa'', 16)
t2 = clock ()
for i in range (10000): a = long (''bbbbaaaa'', 16)
t3 = clock ()
print (t2-t1) / (t3-t2)

it prints out: 23.9673206147

That seems to mean that the int-conversion is 24 times slower than the
long conversion.
The reason is that in Python 2.3 the int-conversion generates
warning messages that you *never* see but that consume a *lot* of CPU. I suppose that Python is getting over-engineered by too professional
programmers. The first signs of decay?



No. I think warning.py needs some _more_ engineering - throw in a dictionary
to speed up repetetive calls or so. In the meantime:

import warnings
def dummy(*args, **kw):
pass
warnings.warn = dummy

Now watch it speed up :)

Peter


On Tuesday 13 July 2004 3:09 pm, Willem wrote:

When I run the follwing code using Python 2.3:

from time import clock
t1 = clock ()
for i in range (10000): a = int (''bbbbaaaa'', 16)
t2 = clock ()
for i in range (10000): a = long (''bbbbaaaa'', 16)
t3 = clock ()
print (t2-t1) / (t3-t2)

it prints out: 23.9673206147

That seems to mean that the int-conversion is 24 times slower than the
long conversion.
The reason is that in Python 2.3 the int-conversion generates
warning messages that you *never* see but that consume a *lot* of CPU.

So it may happen that old code performing well under Python 2.2 suddenly
slows down a considerable amount under Python 2.3 without any percievable
change in the output ...



I don''t think this code did work perfectly well under Python2.2 (see below),
or am I missing something?

Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

for i in range (10000): a = int (''bbbbaaaa'', 16) ....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: int() literal too large: bbbbaaaa



--
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net


Willem wrote:

So it may happen that old code performing well under Python 2.2 suddenly
slows down a considerable amount under Python 2.3 without any percievable
change in the output ...



IMO the speed at which a bunch of invalid conversions are
executed means nothing at all. Could you come up with an example
that show the same symptoms in a meaningful context?

i.


这篇关于int()比Python 2.3中的long()慢24倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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