Can this loop be sped up in pure Python?

Posted by Noctis Skytower on Stack Overflow See other posts from Stack Overflow or by Noctis Skytower
Published on 2011-01-13T03:35:36Z Indexed on 2011/01/13 3:53 UTC
Read the original article Hit count: 230

Filed under:
|
|
|
|

I was trying out an experiment with Python, trying to find out how many times it could add one to an integer in one minute's time. Assuming two computers are the same except for the speed of the CPUs, this should give an estimate of how fast some CPU operations may take for the computer in question.

The code below is an example of a test designed to fulfill the requirements given above. This version is about 20% faster than the first attempt and 150% faster than the third attempt. Can anyone make any suggestions as to how to get the most additions in a minute's time span? Higher numbers are desireable.

EDIT: This experiment is being written in Python 3.1 and is 15% faster than the fourth speed-up attempt.

def start(seconds):
    import time, _thread
    def stop(seconds, signal):
        time.sleep(seconds)
        signal.pop()
    total, signal = 0, [None]
    _thread.start_new_thread(stop, (seconds, signal))
    while signal:
        total += 1
    return total

if __name__ == '__main__':
    print('Testing the CPU speed ...')
    print('Relative speed:', start(60))

© Stack Overflow or respective owner

Related posts about python

Related posts about Performance