You seem to have misinterpreted the function of timeit.timeit
– the idea is that you tell it what to actually time, and it then times it. Normally, it does the thing you’re timing many, many times, so you can get a meaningful average. This means it also needs to provide a ‘setup’ argument, as the list should presumably be different for each test. Refer to the examples in the documentation if you need – I normally find that they’re easy enough to suit to your purpose. I’ve implemented timeit below:
a=int(input("Size of list: "))
n = 100000
setup = "lst = [random.randrange(100) for _ in range(a)]"
time = timeit.timeit("bubblesort(lst)", setup=setup, globals=globals(), number=n)
print("{} sorts took {}s".format(n, time))
Of course, if you’re using IPython you can do something like this:
In [1]: from bubble import bubblesort
In [2]: from random import randrange
In [3]: %timeit bubblesort([randrange(100) for _ in range(50)])
10000 loops, best of 3: 175 µs per loop
Maybe you were expecting timeit() to tell you just how much time has passed. If so, this could be done using the time
module, quite simply.
import time
start_time = time.time()
bubblesort(lst)
time_taken = time.time() - start_time
print("One sort took {}s".format(time_taken))
On another note, I’d recommend using a different name for the argument “Laux”. As you can see, SO’s syntax highlighting thinks it’s a class, as normally anything starting with a capital letter is a class. Laux also doesn’t particularly tell me as an outsider what it is, although I can infer from the name bubblesort.
solved Python can’t implement timeit module [closed]