0

I'm a newbie to python and I have a question that I can't answer it. Please help me!

For example, I have an array in python:

index: 1 | 2 | 3 | 4 | 5 | 6 |

value: a | b | c | d | e | f |

I can read value from the 5th slot of this array by using 2 ways:

  • print array[4]

  • print array[-2]

So what is the fastest way to access into the 5th position on this array?

Thank you so much.

4
  • 1
    Strange question! What is faster: to wink with the left or right eye? Commented Sep 11, 2014 at 16:21
  • 2
    The speed difference ought to be so small that there is no need to care about the difference on the overwhelming majority of cases. Use the one which captures your intuition better. Commented Sep 11, 2014 at 16:23
  • It should be noted that in this case, the lookup should be used based on the intent of the programmer: positive index means that if items are appended to the list, the positive index will still get you the right item; negative index means if new items are inserted somewhere in the list, the intended item still remains at the given index counted from the right side. Commented Sep 11, 2014 at 16:44
  • @Rainy: Yeah helpful information :) But in my case, there is a fixed list that contains more than one million-billion items (10^18 items)! I have to analyse structure of data when it stored in database by using a list! Commented Sep 11, 2014 at 17:51

3 Answers 3

1

I don't actually know. It would seem to me that they're equivalently fast, but that's what profiling is for, so let's do it!

import timeit

setup = '''the_list = ['a','b','c','d','e','f']'''

timeit.timeit('the_list[4]', setup)
timeit.timeit('the_list[-2]', setup)

for me, those values were:

0.12980895690317062

and

0.12949802723105344

Which shows positive indexing ~3/10000 of a second faster over 10,000 iterations.

Sign up to request clarification or add additional context in comments.

2 Comments

@CalvinFroedge I think we reached the same conclusion though. It's just statistical noise.
Indeed, haha. Either way, fun to actually test stuff like this.
0

They're both very close to constant time.

You may find this helpful:

https://wiki.python.org/moin/TimeComplexity

When I run this many times and average the results, a[-2] actually runs a bit faster...but the difference is so small it probably won't matter even on huge data sets.

from datetime import datetime

a = ['a', 'b', 'c', 'd', 'e', 'f']

startTime = datetime.now()

for i in xrange(0,10000000):
    a[4]

print(datetime.now()-startTime)


startTime = datetime.now()

for i in xrange(0,10000000):
    a[-2]

print(datetime.now()-startTime)

1 Comment

Have you ever try to a "big" list? From 10^15 items? Of course it will be loaded on RAM :)
0

Just timeit:

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[4]" 10000000 loops, best of 3: 0.0458 usec per loop

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[4]" 10000000 loops, best of 3: 0.0459 usec per loop

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[4]" 10000000 loops, best of 3: 0.0489 usec per loop

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[-2]" 10000000 loops, best of 3: 0.0448 usec per loop

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[-2]" 10000000 loops, best of 3: 0.045 usec per loop

c:>py -m timeit -s "x=[1,2,3,4,5,6]" "y=x[-2]" 10000000 loops, best of 3: 0.045 usec per loop

Negligible difference. In these timings it looks like the negative value is a few nanoseconds faster (on my system anyway).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.