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