timeit python crashes command line

Happy post 101 !

Update: Timeit has a default and runs the tested method 1 million times!!!

Update: random randint in a function with timeit() seems to be the culprit.  when you build the DLLlist inside the test_bubble_sort()  with a random_list() function that uses random randint, that pushes the numbers onto the list and returns it.... inside the function timeit is running it will fail.  In powershell and the WSL.  importing bubble_sort was not an issue.  I think importing all three may compound the issue.... may try to figure out more ways to test it tomorrow.

I had timeit() python crash my command line.
I'm pretty sure it's because of the number of imports I had in my original file.
My computer is pretty slow by today's standards, and it's memory also low by today's standards.

I CTRL^C during the first 5 runs when it didn't respond and it was in different locations of the function I was timing, or of the timeit(), so I knew it was working.

My suggestion is to try and eliminate imports if you run into this, especially if you have a slow/low mem computer like me.

I could use random randint,  if I did not use it in a function, and manually built the numbers list with it outside of a function.

Here's my modified file that ran:

It was originally for pytest, a .py file, that imported the DLList, random randint, mergesort, quicksort, bubblesort and then the "if __main__" imported timeit.

bubble_sort.py :  

from ex16DLL import*
#from random import randint
#from bubble_sort import bubble_sort



def bubble_sort(numbers):
    # sorts a list of numbers using bubble sort algorithm
    while True:
        # start off assuming it's sorted
        is_sorted = True
        # comparing 2 at a time, skipping ahead
        node = numbers.begin.next
        while node:
            # loop through comparing previous node to the node
            if node.prev.value > node.value:
            # so if they are = no swap will take place
                node.prev.value, node.value = node.value, node.prev.value
                # if swap, it's not sorted.
                is_sorted = False
            node = node.next

        # this is reset at the top, but if swap never happened, it's sorted.
        # breaks from first while,  'while True:'
        # but where will it end, if it swaps?????? oh.....
        # it will keep going through the list until it doesn't have to swap any.
        if is_sorted: break

### random randint without function build
#numbers = DLList()
#x = randint(-10, 10)

#for i in range(0, 5):
    #numbers.push(i + x)

#this will crash it.
#def random_list(somenum):
    #numbers = DLList()
    #for i in range(0, somenum):
        #numbers.push(randint(0, 100))
    #return numbers


numbers = DLList()
numbers.push(30)
numbers.push(4)
numbers.push(7)
numbers.push(300)
numbers.push(1)

def test_bubble_sort():
    #numbers = random_list(5) <-- crash
    bubble_sort(numbers)

if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test_bubble_sort()", setup="from __main__ import test_bubble_sort"))



Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame