source: doc/theses/andrew_beach_MMath/code/cond-catch.py @ 1f11818

Last change on this file since 1f11818 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: 770 bytes
Line 
1#!/usr/bin/env python3
2
3# Conditional Match (or Re-Raise)
4
5from time import thread_time_ns
6
7
8class EmptyException(Exception):
9    pass
10
11
12should_catch = False
13
14
15def main(argv):
16    times = 1
17    if 1 < len(argv):
18        times = int(argv[1])
19    if 2 < len(argv):
20        should_catch = 0 < int(argv[2])
21
22    start_time = thread_time_ns()
23    for count in range(times):
24        try:
25            try:
26                raise EmptyException()
27            except EmptyException as exc:
28                if not should_catch:
29                    raise
30        except EmptyException:
31            pass
32
33    end_time = thread_time_ns()
34    print('Run-Time (s): {:.1f}'.format((end_time - start_time) / 1_000_000_000.))
35
36
37if '__main__' == __name__:
38    import sys
39    main(sys.argv)
Note: See TracBrowser for help on using the repository browser.