dimanche 25 janvier 2015

async calculation of Fibonacci slower than sequence calculation

I'm trying this code for calculation of Fibonacci series:



unsigned long fib_async(unsigned long n) {
if (n<2) {
return n;
}
else {
auto res1 = async(fib_async, n-1);
auto res2 = async(fib_async, n-2);

return res1.get() + res2.get();
}
}


Why is this code slower than classical:



unsigned long fib(unsigned long n) {
return n<2 ? n : fib(n-2) + fib(n-1);
}


?


Synchronous version for n=40 lasts 0.7 seconds on my 8-core i7 CPU. Asynchronous example for n=40 lasts more than 1 minute and it seems to run on one CPU core only.


What am I doing wrong? I'm testing it on a Linux machine with gcc 4.8.4.


Aucun commentaire:

Enregistrer un commentaire