File: fib.cc

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (34 lines) | stat: -rw-r--r-- 714 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <sys/time.h>
#include <stdio.h>

// this should be found in include under 
// the massivethreads installation directory 
// (<prefix>/include)
#include <mtbb/task_group.h>

double cur_time() {
  struct timeval tp[1];
  gettimeofday(tp, NULL);
  return tp->tv_sec + 1.0E-6 * tp->tv_usec;
}

int fib(int n) {
  if (n < 2) return n;
  else {
    mtbb::task_group tg; int x, y;
    tg.run([=,&x] { x = fib(n - 1); });
    y = fib(n - 2);
    tg.wait();
    return x + y;
  }
}

int main(int argc, char ** argv) {
  int n = (argc > 1 ? atoi(argv[1]) : 35);
  while (1) {
    double t0 = cur_time();
    int x = fib(n);
    double t1 = cur_time();
    printf("fib(%d) = %d in %.3f sec\n", n, x, t1 - t0);
  }
}