diff --git a/intro-to-algorithms/ch01.py b/intro-to-algorithms/ch01.py index aced933..38e8f41 100644 --- a/intro-to-algorithms/ch01.py +++ b/intro-to-algorithms/ch01.py @@ -2,7 +2,7 @@ from __future__ import print_function import os import random -import unittest +import timeit DEBUG = os.environ.get('DEBUG') == '1' @@ -32,26 +32,26 @@ def insertion_sort(arr): return arr -class TestStuff(unittest.TestCase): +def main(): + for power in range(1, 5): + to_sort = list(range(10 ** power)) + to_sort_sorted = to_sort[:] + to_sort_reversed = list(reversed(to_sort[:])) - def test_insertion_sort_small(self): - self.assertEqual( - [1, 2, 3, 4, 5, 6], - insertion_sort([5, 2, 4, 6, 1, 3]) - ) + for i in range(3): + random.shuffle(to_sort) - def test_insertion_sort_large(self): - inlist = range(1000) - listcopy = inlist[:] - random.shuffle(inlist) - random.shuffle(inlist) - self.assertEqual(listcopy, insertion_sort(inlist)) + for arr, desc in ( + (to_sort_sorted, 'sorted'), + (to_sort, 'shuffled'), + (to_sort_reversed, 'reversed')): - def test_insertion_sort_large_reversed(self): - inlist = range(1000) - listcopy = inlist[:] - self.assertEqual(listcopy, insertion_sort(list(reversed(inlist)))) + timing = timeit.timeit( + lambda: insertion_sort(arr), + number=10 + ) + print('{} {}: {}'.format(10 ** power, desc, timing)) if __name__ == '__main__': - unittest.main() + main()