How to demonstrate bigO cost of algorithms?

Brian Quinlan brian at sweetapp.com
Wed Jun 2 11:56:12 EDT 2004


Rusty Shackleford wrote:
> Hi -
> 
> I'm studying algorithms and I want to write a python program that
> calculates the actual runtimes.
> 
> I want to increment up some global variable called n in my program so
> that I can see the relationship between the size of the problem and the
> number of steps.
> 
> Here's my clumsy attempt to implement this:
> 
 > [snipped]

What is that ugly try-except block for? All it seems to do is hide 
potential errors.

Anyway, if you are only testing comparison-base sorting algorithms then 
why not create a new class (maybe by subclassing a builtin) and 
implement a __cmp__ method that increments a global whenever called. 
That way you will get an exact count of the number of comparisons.

Cheers,
Brian




More information about the Python-list mailing list