source:
doc/theses/andrew_beach_MMath/code/cond-catch.cpp
@
dd78dbc
Last change on this file since dd78dbc was f79ee0d, 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> | |
[f79ee0d] | 6 | #include <iomanip> |
[ee23a8d] | 7 | |
[f79ee0d] | 8 | using namespace std; |
[ee23a8d] | 9 | using namespace std::chrono; |
[ea593a3] | 10 | |
11 | struct EmptyException : public std::exception {}; | |
12 | ||
13 | bool should_catch = false; | |
14 | ||
15 | int main(int argc, char * argv[]) { | |
16 | unsigned int times = 1; | |
[ee23a8d] | 17 | if (1 < argc) { |
[ea593a3] | 18 | times = strtol(argv[1], nullptr, 10); |
19 | } | |
[ee23a8d] | 20 | if (2 < argc) { |
[11ad42f] | 21 | should_catch = strtol(argv[2], nullptr, 10); |
[ea593a3] | 22 | } |
23 | ||
[ee23a8d] | 24 | time_point<steady_clock> start_time = steady_clock::now(); |
[ea593a3] | 25 | for (unsigned int count = 0 ; count < times ; ++count) { |
26 | try { | |
[f79ee0d] | 27 | try { |
28 | throw EmptyException(); | |
29 | } catch (EmptyException & exc) { | |
30 | if (!should_catch) { | |
31 | throw; | |
32 | } | |
33 | asm volatile ("# catch block (conditional)"); | |
34 | } | |
[ea593a3] | 35 | } catch (EmptyException &) { |
[54651005] | 36 | asm volatile ("# catch block (unconditional)"); |
[ea593a3] | 37 | } |
38 | } | |
[ee23a8d] | 39 | time_point<steady_clock> end_time = steady_clock::now(); |
40 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); | |
[f79ee0d] | 41 | cout << "Run-Time (s): " << fixed << setprecision(1) << duration.count() / 1'000'000'000. << endl; |
[ea593a3] | 42 | } |
Note: See TracBrowser
for help on using the repository browser.