![]() Can we do better?Ĭonsider an array that is extremely large (let's say, so large it would be computationally slow to slice it). Getting Python slices is runtime O(k) where k is the slice, so a sliced rotation is runtime N. ![]() What is the use case? Often, we don't actually need a fully shifted array -we just need to access a handful of elements in the shifted array. This can be translated to Python as follows: def rotate(x, i): Let's view the problem as transforming the array ab into the arrayīa, but let's also assume that we have a function that reverses theĮlements in a specified portion of the array. Jon Bentley in Programming Pearls (Column 2) describes an elegant and efficient algorithm for rotating an n-element vector x left by i positions: I don't know if thats a bug, but I think it's more likely that I missunderstood something here.ĭoes anyone of you know why this happens? Otherlist = <- SHOULD NOT HAVE BIN CHANGED! After passing the content of otherlist to the M圜lass.classlist list, calling the shift_classlist() also changes the otherlist list: The shift_classlist() method executes the same code as my x.insert(0,x.pop())-solution, otherlist is a list indipendent from the class. Print ' otherlist =', otherlist, '<- SHOULD NOT HAVE BIN CHANGED!' # this is where kind of a magic link is created.įor ii in xrange(2): # just to do it 2 times I don't know if this is 'efficient', but it also works: x = ĮDIT: Hello again, I just found a big problem with this solution!Ĭonsider the following code: class M圜lass():ĭef shift_classlist(self): # right-shift-operation It turns out that Kelly Bundy's suggestion tmp = dataĮssentially, perfplot performs the shift for increasing large arrays and measures the time. I also got interested in this and compared some of the suggested solutions with perfplot (a small project of mine). Timeit.timeit(test_append_pop, setup_append_pop) Timeit.timeit(test_roll_alone, setup_np_roll_alone)ġM iterations 4.819645881652832 seconds setup_shift_in_place=""" ![]() Np.roll(l,-1) # implicit conversion of l to np.nparrayġM iterations 6.0491721630096436 seconds setup_np_roll_alone = """ Timeit.timeit(test_deque_rotate_alone, setup_deque_rotate_alone)ġM iterations 27.558452129364014 seconds setup_np_roll_with_create_npa = """ ![]() Timeit.timeit(test_deque_rotate_with_create_deque, setup_deque_rotate_with_create_deque)ġM iterations 0.12380790710449219 seconds setup_deque_rotate_alone = """ Test_deque_rotate_with_create_deque = """ Showing that creating deques from lists is O(n): from collections import dequeīest, others = big_o.big_o(create_deque_from_list, lambda n: big_o.datagen.integers(n, -100, 100)) Just for the sake of illustration, here are some sample timings on 1M iterations: But, if the starting point is a list, the time complexity of using deque.rotate() is O(n). So if you are starting with deque objects, you can deque.rotate() at the cost of O(k). deque.rotate is O(k) (k=number of elements).This can be shown with time complexity alone: If you're starting with a list, l.append(l.pop(0)) is the fastest method you can use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |