Complexity of len() with regard to sets and lists

Firstly, you have not measured the speed of len(), you have measured the speed of creating a list/set together with the speed of len(). Use the –setup argument of timeit: $ python -m timeit –setup “a=[1,2,3,4,5,6,7,8,9,10]” “len(a)” 10000000 loops, best of 3: 0.0369 usec per loop $ python -m timeit –setup “a={1,2,3,4,5,6,7,8,9,10}” “len(a)” 10000000 loops, … Read more

Can an O(n) algorithm ever exceed O(n^2) in terms of computation time?

Asymptotic complexity (which is what both big-O and big-Theta represent) completely ignores the constant factors involved – it’s only intended to give an indication of how running time will change as the size of the input gets larger. So it’s certainly possible that an Θ(n) algorithm can take longer than an Θ(n2) one for some … Read more