本文介绍了警告杀了我的表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下代码: def IntToRandFloat(x): """给定一个32位整数,返回一个漂浮在[-1.0,1.0]""" x = int(x) x = int(x 返回(1.0 - ((x *(x * x * 15731 + 789221)+1376312589)& 0x7fffffff)/1073741824.0) 基本上它是从a直接复制的函数C实现。现在 看起来左移的行导致丢失的比特 或改变符号将在Python 2.4及更高版本中返回长。警告。所有 好​​又好 - 我明确地将它强制转换为int,以便在升级Python时代码 不会中断。我也使用 warnings.filterwarnings(action =''忽略''等)禁用警告。 然而,当我需要配置我的应用程序时,我看到这样的行: 11266617函数调用488.717 CPU秒 ncalls tottime percall cumtime percall 文件名:lineno (功能) 3145728 129.744 0.000 364.845 0.000 terraingen.py:22(IntToRandFloat) 3142872 150.484 0.000 235.101 0.000 warnings.py:24 (警告) 3142872 84.617 0.000 84.617 0.000 warnings.py:59(warn_explicit) 现在我显然可以''我的节目时间几乎有一半 警告消耗我不想看。 它变得陌生。如果我将换档线更改为: x = int(x *(2 ** 13))^ x x = int(x | 0xFFFF) ....每次调用IntToRandFloat 时,它仍在调用''warn'',但我看到没有警告,即使我不知道不要导入警告 并禁用任何警告。我没有其他进口(我可以看到)可能 在幕后禁用警告。 所以我遇到这些问题:警告很慢(即使在禁用的情况下), 有时会发出警告而且我从未看到它们,并且给了 我从未看到警告我不知道如何获得围绕他们。 所以,我的第一个问题是询问是否有一种更有效的方式来禁用警告? 我的第二个是,有一个快速的方法取一个整数,转移 它留下13位(或乘以2 ** 13,无论如何),丢弃任何 多余的位,哪些不会引起警告? 最后,一个建议;如果由于这种转换操作2.4将自动推广到a,标准库 将为我们这些人提供有损移位运算符的C实现 会从快速版本中受益吗?我猜这很长时间来推广 然后让它回到正常的int并不是最快的操作。 - Ben Sizer 解决方案 Ben>所以我有这些问题:警告很慢(即使在禁用时), Ben>有时会发出警告,我从来没有看过它们,并给出了 Ben>我从来没有看到警告,我不知道如何绕过它们。 Ben>所以,我的第一个问题是询问是否有更有效的方式 Ben>禁用警告? 这可能看起来很愚蠢,但切换到Python的CVS版本(又名2.4a0) 应该会有很大的帮助,因为它已经消失了: %python2.3~ / local / bin / timeit.py -s''import sys; sys.path.append(QUOT; /用户/跳过/ TMP");来自警告导入IntToRandFloat''''IntToRandFloat(789221)'' /Users/skip/tmp/warn.py:4:FutureWarning:x<< y丢失位或更改符号将返回很长的Python 2.4及以上 x = int(x 10000循环,最佳3:114每循环usec %python2.4~ / local / bin / timeit.py -s''import sys; sys.path.append(QUOT; /用户/跳过/ TMP");来自warn import IntToRandFloat''''IntToRandFloat(789221)'' 100000循环,最佳3:16.7 usec每循环 Ben>最后,一个建议;如果2.4将引入自动促销 Ben>由于这种转变操作,很长一段时间将标准 Ben>库提供有损移位运算符的C实现 Ben>那些受益于快速版本的我们?我在猜测 Ben>将它推广到很长时间然后让它恢复正常 Ben> int并不是最快的操作。 显然它比导航所有警告 机器要快得多。 ;-) Skip ''或''in in'x = int(x | 0xFFFF)"在任何人选择指出它之前,显然意味着是''和'', 。 :) - Kylotan " Kylotan" < KY ***** @ hotmail.com>在消息中写道 news:15 ************************* @ posting.google.co m ... 我的第二个是,有一个快速的方法取一个整数,移位它留下13位(或乘以2 ** 13,无论如何),丢弃任何多余的比特,哪个不会引起警告? 您是否尝试在转换之前屏蔽高13位*?那么 没有溢出警告?这有什么意义吗? TJR I have the following code: def IntToRandFloat(x):"""Given a 32-bit integer, return a float in [-1.0, 1.0]"""x = int(x)x = int(x << 13) ^ xreturn (1.0-((x*(x*x*15731+789221)+1376312589)&0x7fffffff)/1073741824.0)Basically it''s a function directly copied from a C implementation. Nowit appears that the line with the left-shift causes the "losing bitsor changing sign will return a long in Python 2.4 and up" warning. Allwell and good - I explicitly cast it back to an int so that the codewon''t break when I upgrade Python. I also disable the warning usingwarnings.filterwarnings(action = ''ignore'', etc). However when the time comes to profile my app, I see lines like these: 11266617 function calls in 488.717 CPU secondsncalls tottime percall cumtime percallfilename:lineno(function)3145728 129.744 0.000 364.845 0.000terraingen.py:22(IntToRandFloat)3142872 150.484 0.000 235.101 0.000 warnings.py:24(warn)3142872 84.617 0.000 84.617 0.000warnings.py:59(warn_explicit) Now I obviously can''t afford to have almost half my program timeconsumed by warnings that I don''t want to see. It gets stranger. If I change the shift line to this:x = int(x * (2 ** 13)) ^ xx = int(x | 0xFFFF) ....it''s still calling ''warn'' once for each time IntToRandFloat iscalled, but I see no warning appear, even when I don''t import warningsand disable any. I have no other imports (that i can see) which mightbe disabling a warning behind the scenes. So I have these problems: warnings are slow (even when disabled),sometimes warnings are being issued and I never see them, and giventhat I never see the warnings I don''t know how to get around them. So, my first question is to ask if there is a more efficient way ofdisabling warnings? And my second is, is there a quick way of taking an integer, shiftingit left 13 bits (or multiplying by 2 ** 13, whatever), discarding anyexcess bits, and which won''t cause a warning? Lastly, a suggestion; if 2.4 will introduce an automatic promotion toa long as a result of this shift operation, will the standard libraryprovide a C implementation of the lossy shift operator for those of usthat would benefit from a quick version? I''m guessing that promotingit to a long and then getting it back to a normal int is not exactlythe speediest operation. --Ben Sizer 解决方案 Ben> So I have these problems: warnings are slow (even when disabled),Ben> sometimes warnings are being issued and I never see them, and givenBen> that I never see the warnings I don''t know how to get around them. Ben> So, my first question is to ask if there is a more efficient way ofBen> disabling warnings? This may seem silly, but switching to the CVS version of Python (aka 2.4a0)should help immensely, simply because that is gone: % python2.3 ~/local/bin/timeit.py -s ''import sys ; sys.path.append("/Users/skip/tmp"); from warn import IntToRandFloat'' ''IntToRandFloat(789221)''/Users/skip/tmp/warn.py:4: FutureWarning: x<<y losing bits or changing sign will return a long in Python 2.4 and upx = int(x << 13) ^ x10000 loops, best of 3: 114 usec per loop% python2.4 ~/local/bin/timeit.py -s ''import sys ; sys.path.append("/Users/skip/tmp"); from warn import IntToRandFloat'' ''IntToRandFloat(789221)''100000 loops, best of 3: 16.7 usec per loop Ben> Lastly, a suggestion; if 2.4 will introduce an automatic promotionBen> to a long as a result of this shift operation, will the standardBen> library provide a C implementation of the lossy shift operator forBen> those of us that would benefit from a quick version? I''m guessingBen> that promoting it to a long and then getting it back to a normalBen> int is not exactly the speediest operation. Apparently it''s quite a bit faster than navigating all the warningmachinery. ;-) SkipThe ''or'' in "x = int(x | 0xFFFF)" was obviously meant to be an ''and'',before anybody chooses to point it out. :) --Kylotan"Kylotan" <ky*****@hotmail.com> wrote in messagenews:15*************************@posting.google.co m... And my second is, is there a quick way of taking an integer, shifting it left 13 bits (or multiplying by 2 ** 13, whatever), discarding any excess bits, and which won''t cause a warning? Have you tried masking off the upper 13 bits *before* the shift? So thatthere is no overflow to warn about? Does this make any sense? TJR 这篇关于警告杀了我的表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-21 12:50