1 #!/usr/bin/env python 2 3 """ 4 A simple example of sequential computation using a function, appending results 5 to a list/array. 6 """ 7 8 import time 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) 24 return i * N + j 25 26 # Main program. 27 28 if __name__ == "__main__": 29 30 t = time.time() 31 32 # Initialise an array. 33 34 results = [] 35 36 # Perform the work. 37 38 print "Calculating..." 39 for i in range(0, N): 40 for j in range(0, N): 41 results.append(calculate(i, j)) 42 43 # Show the results. 44 45 for i in range(0, N): 46 for result in results[i*N:i*N+N]: 47 print result, 48 print 49 50 print "Time taken:", time.time() - t 51 52 # vim: tabstop=4 expandtab shiftwidth=4