source: doc/theses/andrew_beach_MMath/code/throw-detor.cpp @ 209dfe2

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

Added asm statements to the exception benchmarks to prevent unwanted optimizations.

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[ea593a3]1// Throw Across Destructor
[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
11struct WithDestructor {
[54651005]12        ~WithDestructor() {
13                asm volatile ("# destructor body");
14        }
[ea593a3]15};
16
17void unwind_destructor(unsigned int frames) {
18        if (frames) {
19                WithDestructor object;
20                unwind_destructor(frames - 1);
21        } else {
22                throw (EmptyException){};
23        }
24}
25
26int main(int argc, char * argv[]) {
27        unsigned int times = 1;
28        unsigned int total_frames = 1;
[ee23a8d]29        if (1 < argc) {
[ea593a3]30                times = strtol(argv[1], nullptr, 10);
31        }
[ee23a8d]32        if (2 < argc) {
[ea593a3]33                total_frames = strtol(argv[2], nullptr, 10);
34        }
35
[ee23a8d]36        time_point<steady_clock> start_time = steady_clock::now();
[ea593a3]37        for (int count = 0 ; count < times ; ++count) {
38                try {
39                        unwind_destructor(total_frames);
40                } catch (EmptyException &) {
[54651005]41                        asm volatile ("# catch block");
[ea593a3]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.