2007-09-16 | paulb | file changeset files shortlog | [project @ 2007-09-16 19:36:23 by paulb] Updated the release notes. |
paulb@109 | 1 | #!/usr/bin/env python |
paulb@109 | 2 | |
paulb@109 | 3 | """ |
paulb@109 | 4 | A simple example of sequential computation using a function. |
paulb@109 | 5 | """ |
paulb@109 | 6 | |
paulb@109 | 7 | import time |
paulb@112 | 8 | #import random |
paulb@109 | 9 | |
paulb@109 | 10 | # Array size. |
paulb@109 | 11 | |
paulb@109 | 12 | N = 10 |
paulb@109 | 13 | delay = 1 |
paulb@109 | 14 | |
paulb@109 | 15 | # Work function. |
paulb@109 | 16 | |
paulb@109 | 17 | def calculate(i, j): |
paulb@109 | 18 | |
paulb@109 | 19 | """ |
paulb@109 | 20 | A supposedly time-consuming calculation on 'i' and 'j'. |
paulb@109 | 21 | """ |
paulb@109 | 22 | |
paulb@112 | 23 | #time.sleep(delay * random.random()) |
paulb@109 | 24 | time.sleep(delay) |
paulb@112 | 25 | return (i, j, i * N + j) |
paulb@109 | 26 | |
paulb@109 | 27 | # Main program. |
paulb@109 | 28 | |
paulb@109 | 29 | if __name__ == "__main__": |
paulb@109 | 30 | |
paulb@109 | 31 | t = time.time() |
paulb@109 | 32 | |
paulb@109 | 33 | # Initialise an array. |
paulb@109 | 34 | |
paulb@109 | 35 | results = [0] * N * N |
paulb@109 | 36 | |
paulb@109 | 37 | # Perform the work. |
paulb@109 | 38 | |
paulb@109 | 39 | print "Calculating..." |
paulb@109 | 40 | for i in range(0, N): |
paulb@109 | 41 | for j in range(0, N): |
paulb@112 | 42 | i2, j2, result = calculate(i, j) |
paulb@112 | 43 | results[i2*N+j2] = result |
paulb@109 | 44 | |
paulb@109 | 45 | # Show the results. |
paulb@109 | 46 | |
paulb@109 | 47 | for i in range(0, N): |
paulb@109 | 48 | for result in results[i*N:i*N+N]: |
paulb@109 | 49 | print result, |
paulb@109 | 50 | print |
paulb@109 | 51 | |
paulb@109 | 52 | print "Time taken:", time.time() - t |
paulb@109 | 53 | |
paulb@109 | 54 | # vim: tabstop=4 expandtab shiftwidth=4 |