0

What is wrong with my setup parameter in the following?

import timeit
import random
from copy import copy

def shortBubbleSort(aList):
    n = len(aList) - 1
    iterating = True
    while n > 0 and iterating:
        iterating = False
        for i in range(n):
            if aList[i+1] < aList[i]:
                iterating = True
                aList[i], aList[i+1] = aList[i+1], aList[i]
        n -= 1
    return aList


L = []
for i in range(1,500):
    L.append(random.randrange(0,1000000))

x = timeit.repeat("bubbleSort(copy(L))", setup="from __main__ import bubbleSort,copy,L",repeat = 100,number = 100)
y = (sum(x)/len(x))*100
print(str(y))

I've also tried the following:

  • setup="from __main__ import bubbleSort,from copy import copy"
  • setup="from __main__ import bubbleSort,copy"
  • setup="from __main__ import bubbleSort"

Traceback as follows:

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\ActiveState\KomodoEdit\7.1\samples\bubbleSort TimeIt.py", line 24, in <module>
    x = timeit.repeat("bubbleSort(copy(L))", setup="from __main__ import bubbleSort,copy,L",repeat = 100,number = 100)
  File "C:\Python32\lib\timeit.py", line 235, in repeat
    return Timer(stmt, setup, timer).repeat(repeat, number)
  File "C:\Python32\lib\timeit.py", line 223, in repeat
    t = self.timeit(number)
  File "C:\Python32\lib\timeit.py", line 195, in timeit
    timing = self.inner(it, self.timer)
  File "<timeit-src>", line 3, in inner
ImportError: cannot import name bubbleSort
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 2
    Well, what makes you think that something is wrong with it? Do you get an error? If so, please show the complete traceback. –  Jan 05 '14 at 17:45
  • ok - will add now ...even though I know it was a typo. – whytheq Jan 05 '14 at 17:53

2 Answers2

0

Probably a typo. Actual function name is shortBubbleSort and you are importing bubbleSort

On my machine, the result was

192.437240362
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

It's a typo. Your function is called shortBubbleSort, not bubbleSort. This works:

x = timeit.repeat("shortBubbleSort(copy(L))", setup="from __main__ import shortBubbleSort,copy,L",repeat = 100,number = 100)
BartoszKP
  • 34,786
  • 15
  • 102
  • 130