source: doc/theses/andrew_beach_MMath/code/cond-catch.cpp@ 37c6f77

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 37c6f77 was 8ee4475, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Conditional matching benchmarks renamed based on type of catch used (catch=recover).

  • Property mode set to 100644
File size: 993 bytes
RevLine 
[ea593a3]1// Conditional Match (or Re-Raise)
[ee23a8d]2#include <chrono>
[ea593a3]3#include <cstdlib>
[ee23a8d]4#include <exception>
5#include <iostream>
6
7using namespace std::chrono;
[ea593a3]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;
[ee23a8d]29 if (1 < argc) {
[ea593a3]30 times = strtol(argv[1], nullptr, 10);
31 }
[ee23a8d]32 if (2 < argc) {
[11ad42f]33 should_catch = strtol(argv[2], nullptr, 10);
[ea593a3]34 }
35
[ee23a8d]36 time_point<steady_clock> start_time = steady_clock::now();
[ea593a3]37 for (unsigned int count = 0 ; count < times ; ++count) {
38 try {
39 cond_catch();
40 } catch (EmptyException &) {
41 // ...
42 }
43 }
[ee23a8d]44 time_point<steady_clock> end_time = steady_clock::now();
45 nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time);
46 std::cout << "Run-Time (ns): " << duration.count() << std::endl;
[ea593a3]47}
Note: See TracBrowser for help on using the repository browser.