Python sortedcontainers is too slow

253 Views Asked by At

#This is my code

from sortedcontainers import SortedList, SortedSet, SortedDict
import timeit
import random


def test_speed1(data):
    SortedList(data)

def test_speed2(data):
    sorted_data = SortedList()
    for val in data:
        sorted_data.add(val)


data = []
numpts = 10 ** 5
for i in range(numpts):
    data.append(random.random())
print(f'Num of pts:{len(data)}')

sorted_data = SortedList()
n_runs=10
result = timeit.timeit(stmt='test_speed1(data)', globals=globals(), number=n_runs)
print(f'Speed1 is {1000*result/n_runs:0.0f}ms')

n_runs=10
result = timeit.timeit(stmt='test_speed2(data)', globals=globals(), number=n_runs)
print(f'Speed2 is {1000*result/n_runs:0.0f}ms')

enter image description here

The code for test speed2 is supposed to take 12~ ms (I checked the setup they report). Why does it take 123 ms (10X slowers)??? test_speed1 runs in 15 ms (which makes sense)

I am running in Conda. The This is where they outlined the performance https://grantjenks.com/docs/sortedcontainers/performance.html

2

There are 2 best solutions below

3
On

You are presumably not executing your benchmark in the same conditions as they do:

  • you are not using the same benchmark code,
  • you don't use the same computer with the same performance characteristics,
  • you are not using the same Python version and environment,
  • you are not running the same OS,
  • etc.

Hence, the benchmark results are not comparable and you cannot conclude anything about the performance (and certainly not that "sortedcontainers is too slow").

Performance is only relative to a given execution context and they only stated that their solution is faster relative to other concurrent solutions.

If you really wish to execute the benchmark on your computer, follow the instructions they give in the documentation.

1
On

"init() uses Python’s highly optimized sorted() function while add() cannot.". This is why the speed2 is faster than the speed3.

This is the answer I got from the developers on the sortedcontainers library.