source: doc/theses/andrew_beach_MMath/code/throw-empty.cpp

Last change on this file was 49b3389, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Follow-up, perventing recursion removal in C++.

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