source:
doc/theses/andrew_beach_MMath/code/cond-catch.cpp
@
98233b3
Last change on this file since 98233b3 was 54651005, checked in by , 3 years ago | |
---|---|
|
|
File size: 1.1 KB |
Rev | Line | |
---|---|---|
[ea593a3] | 1 | // Conditional Match (or Re-Raise) |
[ee23a8d] | 2 | #include <chrono> |
[ea593a3] | 3 | #include <cstdlib> |
[ee23a8d] | 4 | #include <exception> |
5 | #include <iostream> | |
6 | ||
7 | using namespace std::chrono; | |
[ea593a3] | 8 | |
9 | struct EmptyException : public std::exception {}; | |
10 | ||
11 | bool should_catch = false; | |
12 | ||
13 | void throw_exception() { | |
14 | throw EmptyException(); | |
15 | } | |
16 | ||
17 | void cond_catch() { | |
18 | try { | |
19 | throw_exception(); | |
20 | } catch (EmptyException & exc) { | |
[e19fadd] | 21 | if (!should_catch) { |
[ea593a3] | 22 | throw; |
23 | } | |
[54651005] | 24 | asm volatile ("# catch block (conditional)"); |
[ea593a3] | 25 | } |
26 | } | |
27 | ||
28 | int main(int argc, char * argv[]) { | |
29 | unsigned int times = 1; | |
[ee23a8d] | 30 | if (1 < argc) { |
[ea593a3] | 31 | times = strtol(argv[1], nullptr, 10); |
32 | } | |
[ee23a8d] | 33 | if (2 < argc) { |
[11ad42f] | 34 | should_catch = strtol(argv[2], nullptr, 10); |
[ea593a3] | 35 | } |
36 | ||
[ee23a8d] | 37 | time_point<steady_clock> start_time = steady_clock::now(); |
[ea593a3] | 38 | for (unsigned int count = 0 ; count < times ; ++count) { |
39 | try { | |
40 | cond_catch(); | |
41 | } catch (EmptyException &) { | |
[54651005] | 42 | asm volatile ("# catch block (unconditional)"); |
[ea593a3] | 43 | } |
44 | } | |
[ee23a8d] | 45 | time_point<steady_clock> end_time = steady_clock::now(); |
46 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); | |
47 | std::cout << "Run-Time (ns): " << duration.count() << std::endl; | |
[ea593a3] | 48 | } |
Note: See TracBrowser
for help on using the repository browser.