main.cpp 1.03 KB
Newer Older
1
#include <utility>
2
#include <cstdio>
3
#include <chrono>
4 5

#include "context_switcher/context_switcher.h"
6 7 8 9
using namespace context_switcher;
using namespace std;

const size_t NUM_RUNS = 1000;
10 11

// Memory for custom stack and continuation semantics
12 13 14
const size_t STACK_SIZE = 512 * 1;
const size_t NUM_STACKS = 64;
char custom_stacks[NUM_STACKS][STACK_SIZE];
15

16 17 18 19
int fib(int n) {
  if (n <= 1) {
    return 1;
  }
20

21 22 23 24 25 26 27 28
  int a, b;
  enter_context(custom_stacks[n], STACK_SIZE, [n, &a](continuation &&cont) {
    a = fib(n - 1);
    return std::move(cont);
  });
  enter_context(custom_stacks[n], STACK_SIZE, [n, &b](continuation &&cont) {
    b = fib(n - 2);
    return std::move(cont);
29
  });
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  return a + b;
}

volatile int result;
int main() {
  auto start_time = chrono::steady_clock::now();
  for (unsigned int i = 0; i < NUM_RUNS; i++) {
    result = fib(18);
  }
  auto end_time = chrono::steady_clock::now();
  auto time = chrono::duration_cast<chrono::microseconds>(end_time - start_time).count();

  printf("%f", (float) time / NUM_RUNS);

45
  return 0;
46
}