Group Abstract Group Abstract

Message Boards Message Boards

2
|
15.6K Views
|
10 Replies
|
11 Total Likes
View groups...
Share
Share this post:

Comparisons between procedural and functional programming

Posted 7 years ago

I would like to know (for teaching purposes) of examples where the efficiency of functional programming is overwhelmingly greater than procedural programming.

POSTED BY: Tomas Garza
10 Replies

Indeed! Very unlikely...

POSTED BY: Tomas Garza

Far from me the idea of starting a flame war

I think there's a very low chance of that happening on this forum :-) The topic is just not that controversial here.

POSTED BY: Szabolcs Horvát
POSTED BY: Tomas Garza

It is instructive to throw a glance at Rosetta Code: Pick a random programming task, and you will almost always find that Mathematica needs the shortest piece of code. And this is IMHO precisely because of the functional programming paradigm.

POSTED BY: Henrik Schachner

seems to me that functional programming code is easier to debug

POSTED BY: Frank Kampas

How easy one coding style or the other is to debug depends on the background of the coder doing the debugging! If a programmer has spent years writing and debugging procedural coding but only a relatively short time writing functional code, then it may indeed be quicker for that programmer to debug code that is procedural, even though it is much longer (with lots of nested loops). I have seen that phenomenon in action, including by students coming to Mathematica after extensive experience with a procedural language. (I saw the same thing regarding array-oriented programming in APL and J vs. procedural programming in "conventional" languages.)

POSTED BY: Murray Eisenberg

Here are some concrete examples:

In[58]:= array = RandomInteger[10000, 5000000];

In[67]:= RepeatedTiming[
 sum = 0;
 i = 1;
 len = Length[array];
 While[i <= len,
  sum += array[[i]];
  i++
  ];
 sum
 ]
Out[67]= {4.860, 24992515511}

In[68]:= Fold[Plus, 0, array] // RepeatedTiming
Out[68]= {0.25, 24992515511}

In[69]:= Total[array] // RepeatedTiming
Out[69]= {0.0015, 24992515511}

The real reason for why the fast ones are fast is that they run fewer operations that are "atomic" in the Wolfram Language. I mean "atomic" in the sense that Total is a single operation whose internals are not accessible.

POSTED BY: Szabolcs Horvát

Functional programming does not, in general, result in more efficient code. In fact, the opposite is true:

https://en.wikipedia.org/wiki/Functional_programming#Efficiency_issues

What you may be referring to is that functional style is a better fit for the Wolfram Language specifically. When there is a direct functional equivalent for a procedural construct in Mathematica, the functional version will typically be faster. One reason for this is that the functional way to solve a task is often higher level and conceptually closer to the task itself. The procedural way often requires spelling out specific details. There are often multiple ways to do this and not all of them are equally efficient. If the problem is expressed at a higher level, that gives Mathematica more leeway to choose an efficient way to do the computation.

Example:

Sum the elements of an array.

The procedural way is something like this, in pseudocode:

sum = 0
i = 0
while i < length(array):
    sum = sum + array[i]
    i = i+1

This is a very specific way to solve the summing problem. Spelling out all the steps constrains the order of operations. This can't easily be parallelized automatically.

However, if there is a single command to sum a list, that can have a more efficient multi-core implementation.

More generally, if we say that we want to reduce the array (Fold operation) with the + operator, knowing that + is associative, and knowing that evaluating + has no side effects, the problem can be straightforwardly and automatically parallelized. Associativity tells us that the order of operations does not matter, so we are not constrained to sequential evaluation.

Here's a related discussion:

https://mathematica.stackexchange.com/questions/134609/why-should-i-avoid-the-for-loop-in-mathematica

This is not about functional vs procedural, but about why it is better to avoid For in Mathematica. However, many of the things mentioned there illustrate the disadvantages of procedural approaches, or rather low-level approaches.


You might notice that I am not so much talking about functional vs procedural here, but about higher or lower level ways to express a concept. In inherently slow interpreted languages, such as the Wolfram Language, higher level expression typically results in better performance than lower level expression, simply because a low-level expression of an algorithm involves more (slow) interpreted steps, while the high-level expression can make use of highly optimized atomic operations. (In a low-level fast language often the opposite is true.)

I'd say that one reason why functional code is better in such languages is that functional code naturally produces a higher-level expression. Additionally, what Matthew said is that it is often close to the way one thinks.

POSTED BY: Szabolcs Horvát

By efficiency, do you mean the efficiency of the code (performance), or efficiency of the programmer (productivity)?

This has been a very long-standing debate in the programming languages community. There is no definitive answer to your question. Many who advocate for functional or declarative approaches emphasize that languages that adopt that model often are more productive once a programmer has some level of mastery for the language, and often the compilers/interpreters for those languages can achieve respectable performance. That is highly subjective though: it often is nuanced, since one needs to consider the person, what constitutes “mastery”, and the kind of problem they are trying to solve. Regarding performance, it’s also hard to say. On some problems, a functional language can compile to code that meets or exceeds hand tuned code in a classical imperative language. On other problems, the functional languages perform significantly worse. Again, like the productivity topic, it’s highly subjective. It also can be highly dependent on which language and which toolchain one considers.

One thing is for certain though: the functional vs non-functional language question is one of the easiest ways to start a flame war online. People get pretty passionate about their favorite tools. You are more likely to get anecdotal “data” and opinions than objective, generalizable answers to this kind of question.

POSTED BY: Matthew Sottile
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard