source: doc/theses/andrew_beach_MMath/code/throw-finally.py @ 5ca5263

Last change on this file since 5ca5263 was b183717, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Update another file in the exception benchmarks as well as some print formatting.

  • Property mode set to 100755
File size: 809 bytes
Line 
1#!/usr/bin/env python3
2
3# Throw Across Finally
4
5from time import thread_time_ns
6
7
8class EmptyException(Exception):
9    pass
10
11
12def unwind_finally(frames):
13    if 0 < frames:
14        try:
15            unwind_finally(frames - 1)
16        finally:
17            pass
18    else:
19        raise EmptyException()
20
21
22def main(argv):
23    times = 1
24    total_frames = 1
25    if 1 < len(argv):
26        times = int(argv[1])
27    if 2 < len(argv):
28        total_frames = int(argv[2])
29
30    start_time = thread_time_ns()
31    for count in range(times):
32        try:
33            unwind_finally(total_frames)
34        except EmptyException:
35            pass
36
37    end_time = thread_time_ns()
38    print('Run-Time (s): {:.1f}'.format((end_time - start_time) / 1_000_000_000.))
39
40
41if '__main__' == __name__:
42    import sys
43    main(sys.argv)
Note: See TracBrowser for help on using the repository browser.