aboutsummaryrefslogtreecommitdiff
path: root/bindings/python/examples/simple.py
blob: 350c85ae8362956a5b41ca797afbb019555b6ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# simple.py -- basic ppamltracer-python example
# This file is in the public domain.

import os
import sys

from ppamltracer import Tracer

def main():
    # Disable buffering on stdout so we can see the numbers as they are printed.
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
    # Start ppamltracer.
    with Tracer("/tmp/simple_report") as tracer:
        # Register the factorial phase.
        with tracer.create_phase("fact") as phase:
            # Print factorials.
            print "Factorials:",
            for i in range(21):
                print fact(phase, i),
            print
        # Register the Fibonacci phase.
        with tracer.create_phase("fib") as phase:
            # Print Fibonacci numbers.
            print "Fibonacci numbers: ",
            for i in range(24):
                print fib(phase, i),
            print

def fact(phase, n):
    # Record that we're running inside the factorial phase.
    with phase.running():
        # Compute the factorial.
        if n == 0:
            return 1
        else:
            return n * fact(phase, n - 1)

def fib(phase, n):
    # Record that we're running inside the Fibonacci phase.
    with phase.running():
        # Compute the nth Fibonacci number.
        if n == 0 or n == 1:
            return n
        else:
            return fib(phase, n - 1) + fib(phase, n - 2)

if __name__ == '__main__':
    main()