## ← Spin Loop - Intro to Computer Science

• 2 Followers
• 27 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=hBw8qWGHrEs" data-team="udacity"></div> ``` 2 Languages

Showing Revision 5 created 05/25/2016 by Udacity Robot.

1. So, to get a better sense of how timing works,
2. I've defined a procedure spin loop. Spin loop starts by
3. initializing the variable i to zero and then it goes
4. through a loop n times, each time through the loop is
5. just adds one to i. So that'll run for longer,
6. we can, by picking the value of n, make it
7. go through the loop any number of times. So, let's
8. try that. So I'll try running the loop 1,000 times and
9. now we get no result, and the time it took is 0.0001 seconds. So, about a tenth
10. of a millisecond. Let's try it with a
11. larger number of executions, so now I am going
12. to increase the number of iterations by ten,
13. so the value of n's multiplied by ten. And
14. if we see that now, the time is, is higher, the time is increased to about six
15. tenths of a millisecond and we can keep increasing
16. the time, so let's try looping 100,000 times. And
17. we see if the time increases. And the time
18. increased by about a factor of ten. Between looping
19. 10,000 times and looping 100,000 times. And we'll try
20. one more time. Let's go for a million. If
21. we go for a million times, now we're up
22. to 0.05. So, five hundredths of a second, or
23. 55 milliseconds. To loop a million times. Still pretty fast, right? We're going
24. through that loop a million times. It's still much less than a second.
25. What's important is, we can see that well, the time changes depending on
26. the input. As we increase the input
27. to spin loop, the time increases accordingly.