Skip to content

list.reverse(): add slice support #19181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

list.reverse(): add slice support #19181

wants to merge 1 commit into from

Conversation

norov
Copy link

@norov norov commented Mar 26, 2020

Hi all,

In Python, I need a tool to reverse part of a list (tail) quickly.
I expected that

nums[start:end].reverse()

would do it inplace with the performance similar to nums.reverse().
However, it doesn't work at all. The fastest way to reverse a part of
the list that I found is like this:

nums[start:end] = nums[end:start-1:-1]

But it is 30 times slower than pure reverse(). The patch below adds
a region support for the reverse(). It works as fast as I expect.

The test script and results are like this:

exec(open('test.py').read())
nums.reverse() 0.006764888763427734
nums = nums[::-1] 0.10066413879394531
nums.reverse(-L/2) 0.003548145294189453
nums.reverse(L/2, L) 0.003538370132446289
nums = nums[:L/2] + nums[L:L/2-1:-1] 0.19934582710266113
nums[L/2:L] = nums[L:L/2-1:-1] 0.11419057846069336

import time

nums = list(range(10000000))
L = len(nums)
LL = int(L/2)

t = time.time()
nums.reverse()
print('nums.reverse()\t\t\t\t', str(time.time() - t))

t = time.time()
nums = nums[::-1]
print('nums = nums[::-1]\t\t\t', str(time.time() - t))

t = time.time()
nums.reverse(-LL)
print('nums.reverse(-L/2)\t\t\t', time.time() - t)

t = time.time()
nums.reverse(LL, L)
print('nums.reverse(L/2, L)\t\t\t', time.time() - t)

t = time.time()
nums = nums[:LL] + nums[L : LL - 1 : -1]
print('nums = nums[:L/2] + nums[L:L/2-1:-1]\t', time.time() - t)

t = time.time()
nums[LL:L] = nums[L:LL-1:-1]
print('nums[L/2:L] = nums[L:L/2-1:-1]\t\t', time.time() - t)

If there is better way to reverse lists, can someone point me at the right
direction? If not, I'll be happy to fix all existing issues and upstream
this approach.

Thanks,
Yury

Signed-off-by: Yury Norov yury.norov@gmail.com

Hi all,

In Python, I need a tool to reverse part of a list (tail) quickly.
I expected that

	nums[start:end].reverse()

would do it inplace with the performance similar to nums.reverse().
However, it doesn't work at all. The fastest way to reverse a part of
the list that I found is like this:

	nums[start:end] = nums[end:start-1:-1]

But it is 30 times slower than pure reverse(). The patch below adds
a region support for the reverse(). It works as fast as I expect.

The test script and results are like this:
>>> exec(open('test.py').read())
nums.reverse()				 0.006764888763427734
nums = nums[::-1]			 0.10066413879394531
nums.reverse(-L/2)			 0.003548145294189453
nums.reverse(L/2, L)			 0.003538370132446289
nums = nums[:L/2] + nums[L:L/2-1:-1]	 0.19934582710266113
nums[L/2:L] = nums[L:L/2-1:-1]		 0.11419057846069336

import time

nums = list(range(10000000))
L = len(nums)
LL = int(L/2)

t = time.time()
nums.reverse()
print('nums.reverse()\t\t\t\t', str(time.time() - t))

t = time.time()
nums = nums[::-1]
print('nums = nums[::-1]\t\t\t', str(time.time() - t))

t = time.time()
nums.reverse(-LL)
print('nums.reverse(-L/2)\t\t\t', time.time() - t)

t = time.time()
nums.reverse(LL, L)
print('nums.reverse(L/2, L)\t\t\t', time.time() - t)

t = time.time()
nums = nums[:LL] + nums[L : LL - 1 : -1]
print('nums = nums[:L/2] + nums[L:L/2-1:-1]\t', time.time() - t)

t = time.time()
nums[LL:L] = nums[L:LL-1:-1]
print('nums[L/2:L] = nums[L:L/2-1:-1]\t\t', time.time() - t)

If there is better way to reverse lists, can someone point me at the right
direction? If not, I'll be happy to fix all existing issues and upstream
this approach.

Thanks,
Yury

Signed-off-by: Yury Norov <yury.norov@gmail.com>
@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@YuryNorov, @norov

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@tirkarthi
Copy link
Member

This will require an issue in the tracker and a discussion for the change. Please open an issue at https://bugs.python.org

@norov
Copy link
Author

norov commented Mar 27, 2020

This will require an issue in the tracker and a discussion for the change. Please open an issue at https://bugs.python.org

Created:
https://bugs.python.org/issue40088

@rhettinger rhettinger closed this Mar 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants