ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
would be nice to demonstrate it somehow. Or at least explain how did you measure it?
Il write simple benchmark for this, i not shure is correct:
Code:
#include <iostream>
#include <chrono>
constexpr auto fib_constexpr(unsigned int n) -> unsigned int {
return (n <= 1) ? n : fib_constexpr(n - 1) + fib_constexpr(n - 2);
}
auto fib_normal(unsigned int n) -> unsigned int {
return (n <= 1) ? n : fib_normal(n - 1) + fib_normal(n - 2);
}
auto benchmark_fib_constexpr() -> void {
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
unsigned int count = 0;
constexpr unsigned int n = 20;
for(unsigned int i = 0; i < 10;) {
fib_constexpr(n);
++count;
end = std::chrono::system_clock::now();
if(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() >= 1000) {
std::cout<<"In second operation count is: "<<count<<std::endl;
count = 0;
++i;
start = std::chrono::system_clock::now();
}
}
}
auto benchmark_fib_normal() -> void {
auto start = std::chrono::system_clock::now();
auto end = std::chrono::system_clock::now();
unsigned int count = 0;
unsigned int n = 20;
for(unsigned int i = 0; i < 10;) {
fib_normal(n);
++count;
end = std::chrono::system_clock::now();
if(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() >= 1000) {
std::cout<<"In second operations count is: "<<count<<std::endl;
count = 0;
++i;
start = std::chrono::system_clock::now();
}
}
}
auto main() -> int {
std::cout<<"CONSTEXPR FIB BENCHMARK"<<std::endl;
benchmark_fib_constexpr();
std::cout<<"NORMAL FIB BENCHMARK"<<std::endl;
benchmark_fib_normal();
return (0);
}
important is compile with -O2 flag:
Code:
g++ -std=c++11 -O2 test.cpp
then in terminal i have result:
Code:
CONSTEXPR FIB BENCHMARK
In second operation count is: 49968765
In second operation count is: 49981539
In second operation count is: 50005027
In second operation count is: 49964753
In second operation count is: 49948262
In second operation count is: 49992233
In second operation count is: 49954760
In second operation count is: 49976978
In second operation count is: 49989060
In second operation count is: 49987518
NORMAL FIB BENCHMARK
In second operations count is: 131435
In second operations count is: 131457
In second operations count is: 131451
In second operations count is: 130226
In second operations count is: 130350
In second operations count is: 130034
In second operations count is: 129069
In second operations count is: 129363
In second operations count is: 120881
In second operations count is: 126952
Since you defined it as a "constant expression," the compiler doesn't have to generate a function at all.
As it was mentioned (and I guess) compiler can optimize it, so I'm not really sure this is the real reason. Furthermore I don't think an additional function call can cause this difference.
Would be nice to see the object code to check it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.