Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 08-02-2007, 12:57 AM   #1
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Rep: Reputation: 22
functional programming in C++

Hello there, this is not an issue I have, I am more interesting in knowing what you think of the hability of C++ to do functional programming.

Here, I am using the STL as much as I can because it is so convenient, actually. I also (sometimes) like functional programming, like what the std::algorithm + functional can do: clearer code (not always, I know, but for simple things, definitely) and less error prone. However, using algorithms always perform slower than an explicit loop (item 43 of Meyers' Effective STL will explain you with lots of details how the std::algorithm performs faster...... profiling shows me explicit loops are always more efficient!); and I am not even 100% convinced that using iterators instead of in a loop is actually faster.

Now, speed matters in what I am doing (scientific programming) as well as style (I have a student who is supposed to learn something) and obviously I want my calculations to be correct.

Clearer code, good style are easier to debug so now, I use std::algorithm when it actually makes the code simpler. But performance?

As a C++ developper, what do you think of std::algorithm and these issues?
Old 08-02-2007, 03:08 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I've written several intensive algorithms in C++, but I've never used std::algorithm. I can tell you from experience that checked iterators are detrimental to performance when not optimized. These iterators carry a pointer to their owning list so that operations on them verify that their pointer value correlates to the correct list. In reality, optimized iterators can somehow be faster than pointers.

Using template functions and classes don't reduce performance by virtue-of-being alone since they are merely for compile-time expansion. The drag comes from internal checks which allow a function to be generic enough to safely perform the given operation with minimal requirements of arguments and no assumptions made otherwise. I always find it easier and faster to control the context of algorithm function calls so that I can make certain assumptions, and can therefore optimize out certain things.

With C++, you have the convenience of creating a private, potentially unsafe, optimized function while having a public wrapper function which always ensures the function is called in a safe manner. If you know how to write a loop that's safe, I'd choose that over a generic algorithm any day. I'd make it a template, though, and follow the basic STL iterator principles to stay safe when processing an STL container. The detriment caused by using iterators can be more than made up for with any compiler worth anything (such as gcc.) Hope that helps.

PS For any code with looping which has to be fast, I find it best to default to what's essentially C code written in a C++ file to allow simple iterator incrementation.
Old 08-03-2007, 01:33 AM   #3
Registered: Jul 2007
Location: Germany
Distribution: Debian, Gentoo, Mac OS X
Posts: 137

Original Poster
Rep: Reputation: 22
Thank you very much for your answer, it is really helpful. Now I am just trying to "make it work" and mostly do profiling to check my algorithms. So I go for what I find safer. At a later stage, however, I will probably change it and follow your advices.

I appreciate your help.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Functional Programming - Discussion taylor_venable Programming 6 07-30-2006 10:20 PM
LXer: Everything Your Professor Failed to Tell You About Functional Programming LXer Syndicated Linux News 0 01-31-2006 09:31 PM
GuI non functional vadivelsurya Slackware - Installation 2 01-24-2006 10:05 AM
Functional desktop cb9fl Linux - Software 1 05-29-2004 03:19 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

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