LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-13-2024, 04:38 AM   #1
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Rep: Reputation: 0
constexpr and performance result


Hi Why constexpr in function giving extremely high performance when i compare to plain function ?

example:

Code:
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);
}
this constexpr function giving whole different result in performance

Thank You for answer.
 
Old 03-13-2024, 05:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
would be nice to demonstrate it somehow. Or at least explain how did you measure it?
 
1 members found this post helpful.
Old 03-13-2024, 05:57 AM   #3
Mac1ek
LQ Newbie
 
Registered: Jan 2024
Distribution: Debian Linux / Red Hat Enterprise
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pan64 View Post
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
 
Old 03-13-2024, 06:17 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That could have been entirely optimized away. The standard benchark method is calculating fib(largeint) modulo 1000000009
 
Old 03-13-2024, 07:11 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
yes, n=20 is not really useful, try other numbers too (like 1234567890).
 
Old 03-13-2024, 08:43 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Since you defined it as a "constant expression," the compiler doesn't have to generate a function at all.
 
Old 03-13-2024, 11:06 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
Quote:
Originally Posted by sundialsvcs View Post
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.
 
  


Reply

Tags
c++, c++11



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
result of : top -o COMMAND ? explain the result please mike1047 Linux - Newbie 3 10-31-2019 09:18 AM
Help with a simple c++ constexpr problem nkoplm Programming 11 08-08-2013 12:20 AM
am not getting any result when am using 'if', without getting result for below script alavala53 Linux - General 3 10-25-2012 06:00 PM
[SOLVED] Grep for the result of a command within the result of another command jasonws Programming 6 11-18-2010 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration