1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple1.py Sat Sep 15 19:42:24 2007 +0000
1.3 @@ -0,0 +1,52 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of sequential computation using a function, appending results
1.8 +to a list/array.
1.9 +"""
1.10 +
1.11 +import time
1.12 +
1.13 +# Array size.
1.14 +
1.15 +N = 10
1.16 +delay = 1
1.17 +
1.18 +# Work function.
1.19 +
1.20 +def calculate(i, j):
1.21 +
1.22 + """
1.23 + A supposedly time-consuming calculation on 'i' and 'j'.
1.24 + """
1.25 +
1.26 + time.sleep(delay)
1.27 + return i * N + j
1.28 +
1.29 +# Main program.
1.30 +
1.31 +if __name__ == "__main__":
1.32 +
1.33 + t = time.time()
1.34 +
1.35 + # Initialise an array.
1.36 +
1.37 + results = []
1.38 +
1.39 + # Perform the work.
1.40 +
1.41 + print "Calculating..."
1.42 + for i in range(0, N):
1.43 + for j in range(0, N):
1.44 + results.append(calculate(i, j))
1.45 +
1.46 + # Show the results.
1.47 +
1.48 + for i in range(0, N):
1.49 + for result in results[i*N:i*N+N]:
1.50 + print result,
1.51 + print
1.52 +
1.53 + print "Time taken:", time.time() - t
1.54 +
1.55 +# vim: tabstop=4 expandtab shiftwidth=4