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: xrange() optimization
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: loewis Nosy List: loewis, rhettinger
Priority: normal Keywords: patch

Created on 2002-05-02 13:39 by rhettinger, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
range5.diff rhettinger, 2002-05-05 19:11 Defined 'used' and made usage consistent
Messages (8)
msg39819 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-02 13:39
Performance improvement for xrange() when used in 
loops or list(xrange()).  Does NOT change external 
interface or restart characteristics.

Fills the tp_iter and tp_iternext slots to provide 
faster looping than access via sq_item wrapped by an 
iterobject.

Times about 25% faster than the original.  Brings
the performance to within 5 to 7% of range().
msg39820 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-02 20:18
Logged In: YES 
user_id=21627

Can you please update this patch to the current CVS
(rangeobject.c 2.35)?
msg39821 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-04 01:08
Logged In: YES 
user_id=80475

Yes.  See new patch from current CVS rangeobject.c 2.35.

Timings still show 25% improvement coming to within 6% of 
range().  Passes \lib\test\test_builtin.py.

External interface unchanged, so docs left unchanged.
msg39822 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-04 13:31
Logged In: YES 
user_id=21627

As currently implemented, there is an API change:
iter(xrange(10)) does not have a .next method anymore.

When revising the patch, please eliminate the extra 0,
before the new tp_ entries.
msg39823 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-05 03:47
Logged In: YES 
user_id=80475

Done.
msg39824 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-05 17:30
Logged In: YES 
user_id=21627

One more question: what is the exact meaning of the "used"
field? More specifically, why does it set used to 0 when
creating a new range object in _getiter?
msg39825 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2002-05-05 19:11
Logged In: YES 
user_id=80475

Attached revised patch with var definition in a comment.
Altered __getiter to set to one to keep consistent with 
definition.  This improves the routine slightly by 
preventing an anomaly with an obscure series of calls:

x=xrange(3); a=iter(x); b=iter(x); c=iter(b); b.next(); 
print c.next, "this should be zero but isn't"

The revised patch handles the above sequence correctly.

The purpose of having the "used" field is to allow re-use 
of the same object in the following context:

x = xrange(10)
for i in range(1000):
    for j in x:    # the same xrange object gets re-used.
        print i,j

msg39826 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2002-05-08 08:49
Logged In: YES 
user_id=21627

Thanks for the patch; applied as rangeobject.c 2.36.
History
Date User Action Args
2022-04-10 16:05:17adminsetgithub: 36538
2002-05-02 13:39:38rhettingercreate