Liste des Groupes | Revenir à cl c++ |
On Sun, 15 Dec 2024 20:20:42 +0000There are a dozen different ways to calculate Fibonacci numbers, ranging from very fast to very slow - but demonstrating different things. Even your program is going to be very slow compared to just using φⁿ / √5.
Student Project <student@invalid.invalid> wrote:
The constexpr is really very smart because it can speed up algorithmsI didn't see the video (I never see that type of videos), but 270
1000 times according to Dave, Microsoft retired engineer. He has
proved it by creating this video:
>
<https://youtu.be/8-VZoXn8f9U?si=iy1UimoWcaLG31Xi>
>
On my computer it took 270 microseconds to calculate fib(35) like in
his example. It was almost instant at the blink of the eyes.
>D:\CmdLine\C_Cpp\Chrono02>program>
Fibonacci_c: 9227465
Time Taken: 270
D:\CmdLine\C_Cpp\Chrono02>program
Fibonacci_c: 9227465
Time Taken: 257
D:\CmdLine\C_Cpp\Chrono02>program
Fibonacci_c: 9227465
Time Taken: 171
D:\CmdLine\C_Cpp\Chrono02>program
Fibonacci_c: 9227465
Time Taken: 176
Amazing.
>
microseconds sound astonishingly slow for fib(35).
#include <stdio.h>
#include <stdlib.h>
#include <intrin.h>
static long long fib(long n)
{
if (fib <= 0)
return 0;
long long f0 = 0, f1 = 1;
for (long i = 1; i < n; ++i) {
long long f2 = f0 + f1;
f0 = f1;
f1 = f2;
}
return f1;
}
Les messages affichés proviennent d'usenet.