Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 2
    Never would've thought that using np.concatenate() was faster. Thank you Kasramvd! Commented Apr 3, 2017 at 12:25
  • 1
    @Gabriel You're welcome. That's actually because that np.concatenate need really fewer operations for doing this task rather than insert which will find the index first do a lot of checks and creates the new ones. Commented Apr 3, 2017 at 12:27
  • 1
    concatenate is compiled code, insert is a relatively complicated Python function (which you can study with np.source(np.insert)). Commented Apr 3, 2017 at 15:56
  • @hpaulj You mean C is only 5 time faster than python? At first I thought that this might be the reason but the difference in run times seems just like a difference in functionality. Commented Apr 3, 2017 at 17:33
  • After trying to replicate insert for the simplest insertion case, I think most of the extra time is due to its greater generality. The core task of copying several slices of a to the new array takes about the same time as with concatenate. But it spends more time setting things up and manipulating the slicing tuple. Commented Apr 3, 2017 at 20:54