It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
Answer from Zach Kelling on Stack OverflowThis page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)...
It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)...
According to said documentation:
list.index(x)Return the index in the list of the first item whose value is x. It is an error if there is no such item.
Which implies searching. You're effectively doing x in s but rather than returning True or False you're returning the index of x. As such, I'd go with the listed time complexity of O(n).
algorithm - python str.index time complexity - Stack Overflow
Does pop(i) have a Time Complexity of O(n) or O(k)?
python - what is the time complexity of list.index(obj) method? - Stack Overflow
python - What is the time complexity to get the last index for an array? - Stack Overflow
Videos
In that article [1] author goes through the algoritm and explains it. From article:
The function “fastsearch” is called. It is a mix between
Boyer-Moore and Horspool algorithms plus couple of neat tricks.
And from the wiki page of Boyer–Moore–Horspool algorithm [2]:
The algorithm trades space for time in order to obtain an
average-case complexity of O(N) on random text, although
it has O(MN) in the worst case, where the length of the
pattern is M and the length of the search string is N.
Hope that helps!
[1] http://www.laurentluce.com/posts/python-string-objects-implementation
[2] https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm
its a combination of a few algorithms- look at this
Python string 'in' operator implementation algorithm and time complexity
or this
http://effbot.org/zone/stringlib.htm
Time complexity is O(n) . Have a look at the link
http://wiki.python.org/moin/TimeComplexity
It's O(n), also check out: http://wiki.python.org/moin/TimeComplexity
This page documents the time-complexity (aka "Big O" or "Big Oh") of various operations in current CPython. Other Python implementations (or older or still-under development versions of CPython) may have slightly different performance characteristics. However, it is generally safe to assume that they are not slower by more than a factor of O(log n)..
Accessing any array element is in constant time, since it is a known by memory location (i.e. a pointer.)
The array does not need to go through prior elements in order to access the nth element (i.e. it is not like a linked list.) The location of all elements is known before hand and can simply be accessed directly.
Updated thanks to comments.
array[-x] is syntactic sugar for array[len(lst) - x]. So it's still a simple constant access to a pointer and takes no time.
You can see this answer for a bit more info. While it is about C, the concepts should be the same.
Native Python lists are arrays of pointers and accessing any element is O(1).
Note that this is an implementation detail specific to the standard Python implementation, known as CPython. Other language implementations may implement lists differently.
Python does not iterate through lists to find a particular index. Lists are arrays (of pointers to elements) in contiguous memory and so locating the desired element is always a simple multiplication and addition. If anything, list[-1] will be slightly slower because Python needs to add the negative index to the length to get the real index. (I doubt it is noticeably slower, however, because all that's done in C anyway.)
Why not test and see?
import timeit
t=timeit.timeit('mylist[99]',setup='mylist=list(range(100))',number=10000000)
print (t)
t=timeit.timeit('mylist[-1]',setup='mylist=list(range(100))',number=10000000)
print (t)
Of course, as you can see by running this a couple times, there's really no (noticable) difference for the reasons pointed out in the other answers.
On the one hand
must be using a hash-table which will give a time complexity close to O(1). Is that right?
is not quite true. Numpy arrays are basically contiguous blocks of homogeneous memory, with some extra info on the side on dimensions and such. Therefore, the access is O(1), and just involves some trivial math to determine the position within the memory.
On the other hand
indexing must be pretty efficient.
is unfortunately not true at all. Asides from bounds checking (which arrays do), everything involving pure python is extremely inefficient (and accesses involve pure-python calls). Numpy array access is no exception. You should try to use vector operations whenever possible.
There is no hash table involved. Numpy arrays are arrays, just like the name implies, and the address is computed like this:
address of nArray[x, y] = base address + A * x + B * y