1 #!/usr/bin/env python 2 3 """ 4 A simple example of serial computation using map-style processing. 5 See the simple_pmap.py program for the corresponding parallel version. 6 """ 7 8 import time 9 10 # Array size. 11 12 N = 10 13 delay = 1 14 15 # Work function. 16 17 def calculate(t): 18 19 "A supposedly time-consuming calculation on 't'." 20 21 i, j = t 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 sequence = [] 32 for i in range(0, N): 33 for j in range(0, N): 34 sequence.append((i, j)) 35 36 results = map(calculate, sequence) 37 38 print "Time taken:", time.time() - t 39 for i in range(0, N): 40 for result in results[i*N:i*N+N]: 41 print result, 42 print 43 44 # vim: tabstop=4 expandtab shiftwidth=4