Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Believe it or not, after profiling my current code, the repetitive operation of numpy array reversion ate a giant chunk of the running time. What I have right now is the common view-based method:

reversed_arr = arr[::-1]

Is there any other way to do it more efficiently, or is it just an illusion from my obsession with unrealistic numpy performance?

1 Answer

0 votes
by (106k points)

The most efficient way to reverse a numpy array is to use  reversed_arr when you are creating a view into the original array. You can then change the original array, and the view will update to reflect the changes.

You can see the below-mentioned code:-

arr = np.array(some_sequence)

reversed_arr = arr[::-1]

do_something(arr)

look_at(reversed_arr)

do_something_else(arr)

look_at(reversed_arr)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)

Browse Categories

...