[Patches] [ python-Patches-551410 ] xrange() optimization

noreply@sourceforge.net noreply@sourceforge.net
Wed, 08 May 2002 01:49:52 -0700


Patches item #551410, was opened at 2002-05-02 15:39
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=551410&group_id=5470

Category: Core (C code)
Group: Python 2.3
>Status: Closed
>Resolution: Accepted
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Martin v. Löwis (loewis)
Summary: xrange() optimization

Initial Comment:
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().

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-05-08 10:49

Message:
Logged In: YES 
user_id=21627

Thanks for the patch; applied as rangeobject.c 2.36.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-05 21:11

Message:
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



----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-05-05 19:30

Message:
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?

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-05 05:47

Message:
Logged In: YES 
user_id=80475

Done.


----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-05-04 15:31

Message:
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.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-05-04 03:08

Message:
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.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-05-02 22:18

Message:
Logged In: YES 
user_id=21627

Can you please update this patch to the current CVS
(rangeobject.c 2.35)?

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=551410&group_id=5470