[Tutor] simple random string generator

Steven D'Aprano steve at pearwood.info
Tue Sep 18 03:59:47 CEST 2012

```On 18/09/12 06:09, Matthew Dalrymple wrote:
>
> thanks for everyone that gave me a hand...i think i got most of the program
>done... now i just need to time how long it takes to run each
>anagramSolution functions to see which one is faster...now i think i might
>have got it done but for some reason both lines are pretty close to the
>same.... http://pastie.org/4741560 thats my code...could someone let me know
>  if i used time.time right to get accurate timing...

Look at what your timing code does:

start = time.time()
for i in range(10000):
anagramSolution2(word,word)
end1 = time.time()
solu2 = end1 - start

Translated into English:

* start the timer
* iteration 1 begins
* call the anagram function
* grab the ending time
* calculate the difference in time
* iteration 2 begins
* call the anagram function
* grab the ending time
* calculate the difference in time
* iteration 3 begins
* call the anagram function
* grab the ending time
* calculate the difference in time
... and so on ...
* iteration 10000 begins
* call the anagram function
* grab the ending time
* calculate the difference in time

and finally you are done. Do you see what you have done? You calculate
the time difference 10000 times instead of once. What you want is:

* start the timer
* iteration 1 begins
* call the anagram function
* iteration 2 begins
* call the anagram function
* iteration 3 begins
* call the anagram function
... and so on ...
* iteration 10000 begins
* call the anagram function
* grab the ending time
* calculate the difference in time

You should calculate the difference in time ONCE, not 10000 times.

> to me it looks like it should be right but the one line i think
>is supposed to increas exponentially

I'm not sure why you think that. I'm not saying that it shouldn't,
but I honestly can't tell. The code is so convoluted that I'm not
sure what it does without detailed study, but at a quick glance I
can't see anything that would make one of the anagram functions
take O(N**2) time.

--
Steven
```