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