Group Abstract Group Abstract

Message Boards Message Boards

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

Comparisons between procedural and functional programming

Posted 6 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

I greatly appreciate the various answers in response to my question. I am not a computer scientist and parts of those answers are well above my level of understanding. I have been a user of Mathematica for some time, and was in the process of preparing an introductory talk for students when I thought I might benefit from other users experience, looking for a few examples where the power of functional programming could be best shown, both in terms of operational power (i.e. speed of execution, import and export facilities, storing results - not only programs or notebooks, but also intermediate results) and of course the quality of the language and how easy it is to get programs up and running, testing code, combining text, code, images, etc. Far from me the idea of starting a flame war, as Matthew says. I am essentially a Mathematica-oriented practitioner, and rather than trying to promote or sell the advantages of the language (in which I firmly believe, perhaps because I practically know nothing else), I wish to provide clear-cut examples where functional programming is faster to write, easier to understand and very good in performance terms (and also, as Frank says, easier to debug) than procedural or imperative languages. All this said, I must add that reading the replies has been extremely useful and I sincerely thank you all. Szabolcs provides a lot of ideas and useful examples, although some of the references he gives were hard to understand. George's position, I fully agree with. And Henrik, thanks for the link to Rosetta Code: quite interesting.

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

In general, using built-in Wolfram Language functions is going to be faster than rolling your own. This is particularly true for any type of for-next loop construction. The trick is to understand what you want to do in terms of these functions. The utility of Wolfram Language is that you can mix programming styles to match what you want to do.

Also, the "best" code is code where you have to write the least. I have seen this mantra for other languages (objective-c and Swift -- I am/was a Mac developer). The people writing the code behind Wolfram Language probably spend a lot more time understanding the deep details of the computer science behind algorithms, even if the code is eventually written in c or some other imperative language. In a sense, these people are your collaborators, and it makes sense to use the resource.

Just as there are beautiful and ugly proofs in Mathematics, there is beautiful and ugly code. I have written plenty of ugly code, but my experience is that beautiful code is generally more robust and easier to maintain (and understand), and it is worth a minor performance hit. It was worth the time and effort to rewrite the code to make it beautiful.

It also depends on what you are using the code for. For a single use application, the faster you can write good code, the better. For "production" code, robustness wins out over speed in almost all cases. It is only when you are pushing the envelope that execution speed is primary.

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
POSTED BY: Szabolcs Horvát
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