Re: constexpr is really very smart!

Liste des GroupesRevenir à cl c++ 
Sujet : Re: constexpr is really very smart!
De : already5chosen (at) *nospam* yahoo.com (Michael S)
Groupes : comp.lang.c++
Date : 18. Dec 2024, 13:17:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20241218141740.00004649@yahoo.com>
References : 1 2 3
User-Agent : Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
On Wed, 18 Dec 2024 04:45:20 +0000
Student Project <student@invalid.invalid> wrote:

 
<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
#include <iostream>
#include <chrono>
 
int fibonacci(int n)
{
     if (n <= 1)
         return n;
     return fibonacci(n - 2) + fibonacci(n - 1);
}
 
constexpr int fibonacci_c(int n)
{
     if (n <= 1)
         return n;
     return fibonacci_c(n - 2) + fibonacci_c(n - 1);
}
 
int main(void)
{
     // using namespace std::literals::chrono_literals;
     auto start = std::chrono::high_resolution_clock::now();
     constexpr int num = 35;
     /*constexpr*/ int result_c = fibonacci_c(num);
 
     std::cout << "Fibonacci_c " << result_c << "\n";
     std::cout << "Time taken: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()
- start).count() << "\n";
 
     start = std::chrono::high_resolution_clock::now();
     int result = fibonacci(num);
     std::cout << "Fibonacci " << result << "\n";
     std::cout << "Time taken: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()
- start).count() << "\n";
 
}
 
<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 

It is likely that at least part of the time you are measuring cout print
time instead of calculations time you are interested in.
Also, if you are using gcc on Windows then there is a significant chance
that implementation of high_resolution_clock is broken. steady_clock is
more reliable.

Try following:
 volatile dummy = 0;
 auto t00 = std::chrono::steady_clock::now();// first call can be slow
 dummy = 1;
 auto t0 = std::chrono::steady_clock::now();
 dummy = 2;
 auto result_c = fibonacci_c(num);
 dummy = 3;
 auto t1 = std::chrono::steady_clock::now();
 dummy = 4;
 auto result = fibonacci(num);
 dummy = 5;
 auto t2 = std::chrono::steady_clock::now();
 dummy = 3;

 etc...






Date Sujet#  Auteur
15 Dec 24 * constexpr is really very smart!39Student Project
16 Dec 24 +* Re: constexpr is really very smart!35Michael S
16 Dec 24 i+* Re: constexpr is really very smart!8David Brown
16 Dec 24 ii`* Re: constexpr is really very smart!7Michael S
16 Dec 24 ii +* Re: constexpr is really very smart!3David Brown
17 Dec 24 ii i`* Re: constexpr is really very smart!2Michael S
17 Dec 24 ii i `- Re: constexpr is really very smart!1David Brown
18 Dec 24 ii `* Re: constexpr is really very smart!3Tim Rentsch
18 Dec 24 ii  `* Re: constexpr is really very smart!2Michael S
18 Dec 24 ii   `- Re: constexpr is really very smart!1Tim Rentsch
17 Dec 24 i`* Re: constexpr is really very smart!26Tim Rentsch
18 Dec 24 i `* Re: constexpr is really very smart!25Michael S
18 Dec 24 i  +* Re: constexpr is really very smart!13Michael S
19 Dec 24 i  i+* Re: constexpr is really very smart!11Tim Rentsch
20 Dec 24 i  ii`* Re: constexpr is really very smart!10Michael S
20 Dec 24 i  ii `* Re: constexpr is really very smart!9Tim Rentsch
21 Dec 24 i  ii  `* Re: constexpr is really very smart!8Michael S
22 Dec 24 i  ii   `* Re: constexpr is really very smart!7Tim Rentsch
23 Dec 24 i  ii    `* Re: constexpr is really very smart!6Michael S
24 Dec 24 i  ii     `* Re: constexpr is really very smart!5Tim Rentsch
25 Dec 24 i  ii      `* Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)4Michael S
25 Dec 24 i  ii       +* Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)2Michael S
27 Dec 24 i  ii       i`- Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)1Tim Rentsch
27 Dec 24 i  ii       `- Re: Bignum multiplication in Python vs GMP (was: constexpr is really very smart!)1Tim Rentsch
19 Dec 24 i  i`- Re: constexpr is really very smart!1Student Project
18 Dec 24 i  `* Re: constexpr is really very smart!11Tim Rentsch
18 Dec 24 i   `* Re: constexpr is really very smart!10Michael S
19 Dec 24 i    `* Re: constexpr is really very smart!9Tim Rentsch
19 Dec 24 i     `* Re: constexpr is really very smart!8Michael S
20 Dec 24 i      `* Re: constexpr is really very smart!7Tim Rentsch
21 Dec 24 i       `* Re: constexpr is really very smart!6Michael S
21 Dec 24 i        +* Re: constexpr is really very smart!2James Kuyper
22 Dec 24 i        i`- Re: constexpr is really very smart!1Michael S
22 Dec 24 i        `* Re: constexpr is really very smart!3Tim Rentsch
22 Dec 24 i         `* Re: constexpr is really very smart!2Michael S
23 Dec 24 i          `- Re: constexpr is really very smart!1Tim Rentsch
17 Dec 24 `* Re: constexpr is really very smart!3Keith Thompson
18 Dec 24  `* Re: constexpr is really very smart!2Student Project
18 Dec 24   `- Re: constexpr is really very smart!1Michael S

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal