pprocess

examples/simple_map.py

149:eeaa043dbfb1
2008-06-04 paulb [project @ 2008-06-04 22:13:31 by paulb] Added a link to the reference document.
     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 #import random    10     11 # Array size.    12     13 N = 10    14 delay = 1    15     16 # Work function.    17     18 def calculate(t):    19     20     "A supposedly time-consuming calculation on 't'."    21     22     i, j = t    23     #time.sleep(delay * random.random())    24     time.sleep(delay)    25     return i * N + j    26     27 # Main program.    28     29 if __name__ == "__main__":    30     31     t = time.time()    32     33     # Initialise an array.    34     35     sequence = []    36     for i in range(0, N):    37         for j in range(0, N):    38             sequence.append((i, j))    39     40     # Perform the work.    41     42     results = map(calculate, sequence)    43     44     # Show the results.    45     46     for i in range(0, N):    47         for result in results[i*N:i*N+N]:    48             print result,    49         print    50     51     print "Time taken:", time.time() - t    52     53 # vim: tabstop=4 expandtab shiftwidth=4