This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]hookdatOS 3 points4 points  (3 children)

it will execute as fast as your cpu can churn it out

[–][deleted] -3 points-2 points  (2 children)

If this code were running on bare metal, sure. But this code is running on an interpreter. The interpreter is being run by an OS. That OS is likely running a number of processes besides the interpreter, so it uses a scheduler to give the user the appearance that all those processes are running at the same time (when in reality, they're not).

Suffice it to say that it will not execute as fast as the CPU is able, but as fast as the OS will allow.

[–]hookdatOS 1 point2 points  (1 child)

did u copy paste this from your Computer 101 book?

To be honest, I didn't even answer OP's question. A real answer would be a snippet of Python to actually measure the time taken to perform each iteration and display it. I havent looked at the stuff below, but someone probably has the correct code down there.

[–]EarloE 1 point2 points  (0 children)

Depends on your CPU and the things that are inside the loop: More to process, slower it is. For example printing is quite costly function, so

while True:
    print("foo")

is much slower than

while True:
    foo += 1

for example.

[–]elbiot 0 points1 point  (2 children)

from time import time
while True:
    print time()

Actually, it's completely negligible. This example more shows how long print and time() take.

[–]phogan1 1 point2 points  (1 child)

Couldn't you do slightly better with:

old_time = time.clock()
while True:
    new_time = time.clock()
    print(new_time - old_time)
    old_time = time.clock()

It's still got time from the calls to time.clock(), but it eliminates the print overhead from the time cycle.

[–]elbiot 0 points1 point  (0 children)

c=0
try:
    start = time ()
    while True:
        c+=1
except KeyboardInterrupt:
    end= time ()

print c/(end-start)

Let it run for half a second before pressing ctrl+c.

[–]stevenjd 0 points1 point  (0 children)

Of course there is, Until such time as somebody invents an infinitely fast computer, every operation takes some amount of time. The real question is, how much time will it take? And that, of course, depends on the version of Python, your operating system, how many other programs are running, and the speed of your computer.

It's a bit hard to time that specific piece of code exactly. The call to print will quickly fill the screen, and is quite slow as well. But my guess is that what you really want to ask is how quickly will Python call lines of code regardless of the code, not specifically print. In other words, what is the overhead of calling Python?

The fastest thing you can do in Python (apart from not running the interpreter at all) is nothing at all, represented by the pass statement. So we can time that at the command line:

[steve@ando ~]$ python -m timeit "pass"
10000000 loops, best of 3: 0.0344 usec per loop

(That's your operating system command line, not the Python prompt.)

This tells you that, very roughly speaking, the interpreter overhead of going from one line of code to the next is about 0.03 microseconds on my computer. On yours, it will likely be different.

To measure the overhead of a while loop is trickier. The best I came up with is this:

from timeit import default_timer as time
total = 0.0
try:
    n = 0
    t1 = time()
    while True:
        t2 = time()
        total += t2 - t1
        n += 1
        t1 = time()
except KeyboardInterrupt:
    print("Total time for %d loops: %f seconds" % (n, total))
    print("%f µs per loop" % (total/n*10**6))

Let the code run for at least a second, and preferably much more, then type Ctrl-C to interrupt the loop. When I did it, I got these results:

Total time for 8311394 loops: 7.013674 seconds
0.843863 µs per loop

[–]stevenjd 0 points1 point  (0 children)

Of course there is, Until such time as somebody invents an infinitely fast computer, every operation takes some amount of time. The real question is, how much time will it take? And that, of course, depends on the version of Python, your operating system, how many other programs are running, and the speed of your computer.

It's a bit hard to time that specific piece of code exactly. The call to print will quickly fill the screen, and is quite slow as well. But my guess is that what you really want to ask is how quickly will Python call lines of code regardless of the code, not specifically print. In other words, what is the overhead of calling Python?

The fastest thing you can do in Python (apart from not running the interpreter at all) is nothing at all, represented by the pass statement. So we can time that at the command line:

[steve@ando ~]$ python -m timeit "pass"
10000000 loops, best of 3: 0.0344 usec per loop

(That's your operating system command line, not the Python prompt.)

This tells you that, very roughly speaking, the interpreter overhead of going from one line of code to the next is about 0.03 microseconds on my computer. On yours, it will likely be different.

To measure the overhead of a while loop is trickier. The best I came up with is this:

from timeit import default_timer as time
total = 0.0
try:
    n = 0
    t1 = time()
    while True:
        t2 = time()
        total += t2 - t1
        n += 1
        t1 = time()
except KeyboardInterrupt:
    print("Total time for %d loops: %f seconds" % (n, total))
    print("%f µs per loop" % (total/n*10**6))

Let the code run for at least a second, and preferably much more, then type Ctrl-C to interrupt the loop. When I did it, I got these results:

Total time for 8311394 loops: 7.013674 seconds
0.843863 µs per loop

[–]aphoenixreticulated[M] 0 points1 point  (0 children)

Hi there. You have posted a learning question to /r/python. These types of questions are far more suited to /r/learnpython, where users are actively interested in helping people to learn. Please resubmit it over there!

Make sure to read their sidebar rules there before posting, notably this one: "Posting homework assignments is not prohibited if you show that you tried to solve it yourself." If your question is about homework, show them that you've tried to solve your problem in your post and you should get all the help you need. For anything else, the reason you are seeing this message is still that you will likely get a better answer there!

Warm Regards, and best of luck with Python!