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

ast-experimental
Last change on this file since 427a577 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
RevLine 
[ea593a3]1// Throw Across Empty Function
[ee23a8d]2#include <chrono>
[49b3389]3#include <cstdio>
[ea593a3]4#include <cstdlib>
[ee23a8d]5#include <exception>
6#include <iostream>
[f79ee0d]7#include <iomanip>
[ee23a8d]8
[f79ee0d]9using namespace std;
[ee23a8d]10using namespace std::chrono;
[ea593a3]11
12struct EmptyException : public std::exception {};
13
14void unwind_empty(unsigned int frames) {
15        if (frames) {
16                unwind_empty(frames - 1);
[49b3389]17                if (-1 == frames) printf("~");
[ea593a3]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;
[ee23a8d]26        if (1 < argc) {
[ea593a3]27                times = strtol(argv[1], nullptr, 10);
28        }
[ee23a8d]29        if (2 < argc) {
[ea593a3]30                total_frames = strtol(argv[2], nullptr, 10);
31        }
32
[ee23a8d]33        time_point<steady_clock> start_time = steady_clock::now();
[ea593a3]34        for (unsigned int count = 0 ; count < times ; ++count) {
35                try {
36                        unwind_empty(total_frames);
37                } catch (EmptyException &) {
[54651005]38                        asm volatile ("# catch block");
[ea593a3]39                }
40        }
[ee23a8d]41        time_point<steady_clock> end_time = steady_clock::now();
42        nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time);
[f79ee0d]43        cout << "Run-Time (s): " << fixed << setprecision(1) << duration.count() / 1'000'000'000. << endl;
[ea593a3]44}
Note: See TracBrowser for help on using the repository browser.