This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Simplify logic in random.py
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: calvin, mdehoon, rhettinger, tim.peters
Priority: normal Keywords: patch

Created on 2005-04-28 05:31 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
random.diff rhettinger, 2005-04-28 05:31 diff
Messages (9)
msg48265 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-28 05:31
Simplify the logic in several methods for better
readability, maintainability, and speed:

* while True --> while 1
* apply de'morgan's theorem to simplify compound
conditionals
* factor out repeated test:  (p>1.0) and (p <= 1.0)
* replace pow() with **
msg48266 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2005-04-29 05:42
Logged In: YES 
user_id=488897

* I don't see how "while 1" is more readable than "while
True". Note also that in older Python versions, this
actually was "while 1"; it was changed to "while True" in
revision 1.39. I can't recommend changing it back.

* This pertains to line 424. I agree that the line in the
patch is more readable and perhaps faster (though marginally
so).

* I agree with you that there is a repeated test in the
current code. In the patch, I would stick with pow() instead
of ** (see my next comment)

* Unnecessary change. Some people like pow(), others like
**. I see no reason to change.

General comment: It is usually better to have different
changes in separate patches. True, the changes in the patch
all apply to random.py, but otherwise they don't have much
in common.

(I'm just a patch reviewer, not an official Python
developer, so you don't need to take my comments too seriously.)
msg48267 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-29 06:12
Logged In: YES 
user_id=80475

Thanks for looking at the patch.  What I really need is for
a second person to verify that the transformations are
semantically neutral (no change in overall logic).

FWIW, I was the one who did rev 1.39.  It turns out that it
was a premature modernization that cost some performance. 
Only the while 1 form is optimized by CPython, Jython, and
Pysco.

Have to disagree with you on pow(). The operator form runs
faster and corresponds more closely to the way formulas are
written in math textbooks and in other languages (except C
which doesn't have a pow operator).  This is the reason we
write a+b instead operator.add(a, b).

If you can a chance, please look closely at the logic
transformations to make sure I did them accurately.  Thanks
again.
 
msg48268 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2005-04-30 05:07
Logged In: YES 
user_id=31435

Raymond, I checked the semantics, and aver that all the 
transformations here are semantically correct.  Marked as 
Accepted -- go for it.
msg48269 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2005-04-30 05:29
Logged In: YES 
user_id=488897

OK, if ** and "while 1" give better performance than pow and
"while true", I'm all for it.
To double-check if the logic is correct, I generated a
million variates with vonmisesvariate and gammavariate with
and without the patch, for various values for the
parameters. I found no problems with it.
msg48270 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-30 09:03
Logged In: YES 
user_id=80475

Thanks.
Checked in as Lib/random.py 1.71
msg48271 - (view) Author: Bastian Kleineidam (calvin) Date: 2005-05-02 14:00
Logged In: YES 
user_id=9205

I agree with mdehoon. Why is "while 1" faster than "while
True"? I think "while True" is more readable. So instead of
replacing with "while 1" the speed issue itself should be fixed.
msg48272 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-05-04 13:32
Logged In: YES 
user_id=80475

Calvin, that won't work.  The "while 1" is optimized because
1 is a constant.  In contrast, "while True" cannot be
precalculated because True is not a constant.
msg48273 - (view) Author: Bastian Kleineidam (calvin) Date: 2005-05-04 14:11
Logged In: YES 
user_id=9205

Oh, I did not realize that True/False are not constants.
Grr, I can even do this:
>>> True, False = False, True
>>> True
False
>>> False
True

This could be handy for the pyobfuscation project :)
History
Date User Action Args
2022-04-11 14:56:11adminsetgithub: 41917
2005-04-28 05:31:30rhettingercreate