source: doc/theses/andrew_beach_MMath/code/cond-match.cpp @ ee23a8d

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ee23a8d was ee23a8d, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Added duration information (in nanoseconds) to EHM benchmarks.

  • Property mode set to 100644
File size: 1.0 KB
Line 
1// Conditional Match (or Re-Raise)
2#include <chrono>
3#include <cstdlib>
4#include <exception>
5#include <iostream>
6
7using namespace std::chrono;
8
9struct EmptyException : public std::exception {};
10
11bool should_catch = false;
12
13void throw_exception() {
14        throw EmptyException();
15}
16
17void cond_catch() {
18        try {
19                throw_exception();
20        } catch (EmptyException & exc) {
21                if (should_catch) {
22                        throw;
23                }
24        }
25}
26
27int main(int argc, char * argv[]) {
28        unsigned int times = 1;
29        unsigned int total_frames = 1;
30        if (1 < argc) {
31                times = strtol(argv[1], nullptr, 10);
32        }
33        if (2 < argc) {
34                total_frames = strtol(argv[2], nullptr, 10);
35        }
36
37        time_point<steady_clock> start_time = steady_clock::now();
38    for (unsigned int count = 0 ; count < times ; ++count) {
39        try {
40                        cond_catch();
41                } catch (EmptyException &) {
42                        // ...
43                }
44    }
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;
48}
Note: See TracBrowser for help on using the repository browser.