timing - Python can't implement timeit module -


this basic exercise. don't know hot implement timeit module correctly. keep recieving syntax errors

import timeit import random def bubblesort(laux):     in range(len(laux)):      exchange = false      j in range(len(laux)-1):          if laux[j] > laux[j+1]:              tmp = laux[j+1]              laux[j+1] = laux[j]              laux[j] = tmp              exchange = true     if not exchange:          break return(laux)  a=int(input("size of list: ")) lst = [0]*a in range(a):     lst[i] = random.randint(1,100) print(bubblesort(lst)) print(timeit.timeit() 

you seem have misinterpreted function of timeit.timeit - idea tell time, , times it. normally, thing you're timing many, many times, can meaningful average. means needs provide 'setup' argument, list should presumably different each test. refer examples in documentation if need - find they're easy enough suit purpose. i've implemented timeit below:

a=int(input("size of list: "))  n = 100000  setup = "lst = [random.randrange(100) _ 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 can this:

in [1]: bubble import bubblesort  in [2]: random import randrange  in [3]: %timeit bubblesort([randrange(100) _ in range(50)]) 10000 loops, best of 3: 175 µs per loop 

maybe expecting timeit() tell how time has passed. if so, done using 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 note, i'd recommend using different name argument "laux". can see, so's syntax highlighting thinks it's class, starting capital letter class. laux doesn't particularly tell me outsider is, although can infer name bubblesort.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -