Alex Rivera | Logout

What is the run time of shift/unshift in a ruby array

Asked 2011-12-02T07:26:40.127
18

Does anyone know how efficient shift and unshift are in a ruby array?

Deleting from the beginning of an array and having to move every element in memory can become very inefficient. I assume ruby does this some other way.

Any info on the following would be helpful:
- Algorithmic runtime
- Implementation
- General efficiency
- Would shift/unshift be acceptable to use for a queue (in something like C++ this would not)

Thank you!

Edit
Report

1 Answer

2

According to this article, it appears that it doesn't really shift at all, just increment a pointer and returns that. So in terms of efficiency it's ridiculously efficient (O(1)). However the article mentions a potential memory leak which may or may not be present in the more recent releases.

answered 2011-12-02T07:35:29.497

Your Answer