source: doc/theses/andrew_beach_MMath/code/throw-detor.cpp @ 47e413b

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

Added duration information (in nanoseconds) to EHM benchmarks.

  • Property mode set to 100644
File size: 1023 bytes
Line 
1// Throw Across Destructor
2#include <chrono>
3#include <cstdlib>
4#include <exception>
5#include <iostream>
6
7using namespace std::chrono;
8
9struct EmptyException : public std::exception {};
10
11struct WithDestructor {
12        ~WithDestructor() {}
13};
14
15void unwind_destructor(unsigned int frames) {
16        if (frames) {
17                WithDestructor object;
18                unwind_destructor(frames - 1);
19        } else {
20                throw (EmptyException){};
21        }
22}
23
24int main(int argc, char * argv[]) {
25        unsigned int times = 1;
26        unsigned int total_frames = 1;
27        if (1 < argc) {
28                times = strtol(argv[1], nullptr, 10);
29        }
30        if (2 < argc) {
31                total_frames = strtol(argv[2], nullptr, 10);
32        }
33
34        time_point<steady_clock> start_time = steady_clock::now();
35        for (int count = 0 ; count < times ; ++count) {
36                try {
37                        unwind_destructor(total_frames);
38                } catch (EmptyException &) {
39                        // ...
40                }
41        }
42        time_point<steady_clock> end_time = steady_clock::now();
43        nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time);
44        std::cout << "Run-Time (ns): " << duration.count() << std::endl;
45}
Note: See TracBrowser for help on using the repository browser.