Wednesday, September 07, 2011

C++ vs Fortran

I've been wanting to put together a blog post about Fortran for a while now, but it never made it to the top of my list of priorities. I'm happy to see that someone named eric_t went ahead and put together a really nice page:

https://bitbucket.org/eric_t/modern-fortran/wiki/Home

So, predictably, the C++ mob showed up with their pitchforks to argue against the use of Fortran. (Actually, a lot of them were arguing against FORTRAN, demonstrating their ignorance.)

As someone who has used both C++ and Fortran for numerical programming, it's hard for me to think of a good reason to use C++ for this stuff.

Let me start out by explaining my perspective, which is what rarely happens in programming language debates. I write numerical programs that do statistical analysis, simulations, and various types of numerical analysis. Most of my programs are less than 5000 lines.

I don't know or care about comparisons of C++ against Fortran for 50 developers working with a codebase of a million lines. I work together with only a few others. I have no interest at all in software development. I write programs as necessary to feed myself.

C++ comes with the mother of all learning curves. I'm not talking about the fact that there's no complexity too large to fit into a C++ program (the language is more than happy to accommodate any desired level of complexity). Though of course that is a problem.

I'm talking about the ability to do basic things like linear algebra or writing functions. A Matlab user can convert to Fortran in about 10 minutes. Obviously you won't know very much, but in the time it takes to get used to the idea of compiling a program and specifying types for your variables, you can define two matrices and do a matrix multiplication in Fortran. The language was written for scientific computations. You can take a look at the gfortran manual to see all the built-in goodies that come with a Fortran compiler.

The bare minimum for doing the same exercise in C++ is that you have to get your hands on a library that does what you want. So you have to know about the existence of Eigen or another libary. Then you have to figure out how to call it. Then you have to figure out how to link to an external library. And the documentation is probably not that helpful. There are friendly C++ users for sure. It's just that you won't have a clue what they're telling you to do. They'll be telling you to set up a makefile. They'll be recommending that you look at Boost and talking about how great it is and you'll be all WTF.

All this mess because you wanted to try out C++ to do a simple matrix multiplication. In Matlab it was just a few lines. In C++ it's a month-long project.

And before you use that C++ library, are you really sure you won't end up with a memory leak? Do you even know what a memory leak is? It probably won't matter for your simple matrix multiplication, but it's real comforting to know that you should worry about such things in order to write such a simple program, isn't it?

Now that the numerical version of "Hello, World!" is out of the way, you can get on to real work. You need to start with a tutorial. Not a complete introduction to the language, of course, just a tutorial that will give you a basic understanding of the language.

An excellent tutorial (that has taught me a great deal) is C++ Primer Plus. I just pulled the Fifth Edition off my shelf. It's okay to just read through Chapter 17. You don't even have to read the appendices. The first 1039 pages of the book is enough for now.

You will have worked through Chapter 4, including the section on "Pointers and the Free Store" and "Pointers, Arrays, and Pointer Arithmetic". You'll have learned about "Dereferencing Pointers". You'll get to work through well-designed syntax like (page 308)

int sum(int (*ar2)[4], int size);

or (page 309)

*(*(ar2 + r) + c)

You'll learn about "Memory Models and Namespaces" in chapter 9. The topics include material useful to newbies like "Automatic Variables and the Stack".

Further along (page 677) you'll learn about "Inheritance and Dynamic Memory Allocation".

Maybe you can just learn the language a little bit at a time. Here's the thing. You really can't. You have to know a lot to read code written by others and to use libraries written by others. Complexity is never hidden behind the scenes with C++. You have to understand memory management before you write your first line of code.

You might think you'll just call libraries so you'll be okay. That's not programming in C++, but let's pretend it is. An easy library (written in C) to call is the GSL. You want to solve a system of equations. Here's the function call:

int (* f) (const gsl_vector * x, void * params, gsl_vector * f)
This is an example of "easy" in C++.

Then when you're done with your tutorial, at a minimum, you need to learn about templates and the STL. And, although not strictly necessary, you should pick up some books by Scott Meyers so you can learn about pitfalls of the language.

If you think I'm just some idiot blogger who doesn't know anything about programming, and don't want to take my word that C++ is too complex to be a reasonable language choice, take a look at this blog post:

http://gigamonkeys.wordpress.com/2009/10/16/coders-c-plus-plus/

It was not a group of mediocre developers too lazy to learn a programming language that he interviewed. From my perspective, Ken Thompson's comment is the most relevant:

"It certainly has its good points. But by and large I think it’s a bad language. It does a lot of things half well and it’s just a garbage heap of ideas that are mutually exclusive. Everybody I know, whether it’s personal or corporate, selects a subset and these subsets are different. So it’s not a good language to transport an algorithm—to say, “I wrote it; here, take it.” It’s way too big, way too complex."

If you can program in Matlab, you can program in Fortran. The same is not true for C++. The complexity is not optional: you just can't interact with the outside world without bumping into it.

The advice to use C++ goes something like this:
1. Spend ten years learning to write C++ programs.
2. Start writing all the programs that you could have been writing ten years ago using Fortran.

I understand what these C++ developers are going through. If I invested ten years of my life learning nearly 1% of the C++ language, I would be upset to find out that those ten years were wasted. I'd be looking for some way to make it feel as though I didn't flush ten years of my working life down the toilet.

There are better choices. For many things, Fortran is an excellent choice. It even has recursive functions and lets you specify that a function is pure. It's not Lisp but that does offer a bit of comfort to someone like me.

If you like the object oriented side of things, Java has closed the speed gap a lot (not that I like Java, but being a better choice than C++ is a low standard). There's also D and Objective-C. The more I use D the more I like it. You really should have your head examined if you have the choice between D and C++ and you choose C++ because you think it's a better language. There might be a different reason to use C++ (like, you're too stubborn to admit that your favorite language sucks) but it's definitely not the case that C++ is a better language.

There are languages like Scala (I've played with it a little but not done any real work, so I can't say much about it) and Clojure that have access to the JVM. You can do the performance critical parts in Java if necessary.

LuaJIT2 is really fast, and the upcoming GSL Shell will be based on LuaJIT2 (at least that's my understanding). Even the current version of GSL Shell does well for speed. PyPy is constantly improving as well. Both are excellent, if somewhat incomplete, choices.

C++? No way. There might be a few cases in which C++ is the best choice, but given the time investment required to learn C++, that is a very small set. If you factor in the time to learn the language, write/debug your program, revisit the program later to fix bugs/add features, and then compile and run programs, even pure interpreted Ruby is likely to be a better choice.

Update: I just watched an excellent presentation by Herb Sutter:

http://channel9.msdn.com/posts/C-and-Beyond-2011-Herb-Sutter-Why-C

He definitely makes a strong case for C++. I don't disagree with much of what he said, except that I think he overstates the speed gap for many tasks between C++ and languages like Java, though for the applications he's talking about the difference still matters. Interestingly, for those applications, developer time is almost irrelevant.

He never says C++ has replaced Fortran. In fact, at the beginning of the talk, he specifies that the three languages that can deliver the needed performance are C, C++, and Fortran. Later at that same conference, they talked about D:

http://channel9.msdn.com/posts/Scott-Meyers-Andrei-Alexandrescu-and-Herb-Sutter-C-and-Beyond

38 comments:

Anonymous said...

exactly, please post more such that ignorance of people can be discarded, people think that c++ is ultra-modern language with 'advanced features' such object orientation compared to adhoc language of antiquity FORTRAN. but fortran 95 is much cleaner and nicer when you say Fortran always emphasize fortran 95 and above (As you have in may places)

cheers

Anonymous said...

I wouldn't worry about extolling the benefit so fortran. Enjoy this time to master it's best features now so you can be the one getting the good jobs before the rest of the monkeys do. Let them spend many restless hours destroying their vision slumped over a computer programming C++ tutorials where half the features will never be used in numerical professions.

Unknown said...

Nice post. I switched to Fortran from C++ myself about a year ago. I've created similar reference pages for modern Fortran here:

http://fortran90.org/

Let me know if you have any comments.

Anonymous said...

Good web site you have here.. It's difficult to find quality writing like yours nowadays. I truly appreciate people like you! Take care!!
Also visit my webpage ; http://www.Every-ways.com/

Anonymous said...

Its such as you learn my thoughts! You seem to understand so much approximately this, like you wrote the ebook in
it or something. I believe that you just can do with some % to pressure the message house a little bit, however instead of that, that is great blog. A great read. I will definitely be back.

My weblog to quit smoking

Anonymous said...

Do you have a spam problem on this website; I also am
a blogger, and I was curious about your situation; we have
created some nice practices and we are looking to exchange
solutions with other folks, why not shoot me an
e-mail if interested.

Look at my web site: make money from home

Anonymous said...

I am really enjoying the theme/design of
your website. Do you ever run into any web browser compatibility problems?

A couple of my blog visitors have complained about my site not operating correctly in Explorer but looks great in Opera.
Do you have any ideas to help fix this issue?

Look at my web-site ... expert link builder

Anonymous said...

WOW just what I was looking for. Came here by searching for make money online opportunity

Feel free to visit my web site; make money online For newbies

Anonymous said...

Hello, i believe that i noticed you visited my web site thus i came to
go back the desire?.I am trying to find issues to improve my site!

I suppose its ok to make use of a few of your
concepts!!

Feel free to surf to my weblog - discount shopping

Anonymous said...

Great post. I used to be checking constantly this weblog and I am inspired!

Very helpful info specifically the remaining part :
) I care for such info much. I was looking for this particular information for a long time.
Thank you and good luck.

my web page ... access power must

Anonymous said...

Highly descriptive article, I loved that bit.
Will there be a part 2?

Here is my web site ... cosplay costumes

Anonymous said...

Nice blog here! Also your website lots up very fast!
What host are you the use of? Can I get your associate link in your host?
I desire my website loaded up as quickly
as yours lol

Also visit my web site smoking cessation

Anonymous said...

We absolutely loѵe youг blog and find
most of yοur post's to be precisely what I'm loοking fοr.
Does one offеr guest writers to write cοntent
in yоur casе? I wοuldn't mind writing a post or elaborating on a few of the subjects you write concerning here. Again, awesome web log!

Feel free to visit my web-site - how to find ppl on facebook using email

Anonymous said...

Do you mind if I quote a couple of your posts as long as I provide credit and sources
back to your weblog? My blog is in the very same niche
as yours and my visitors would genuinely benefit from some of
the information you provide here. Please let me know if this okay
with you. Many thanks!

Here is my page :: arabica coffee beans

Anonymous said...

qsciyrs [url=http://www.guccisprings.com/]グッチ アウトレット[/url] ihiezss vzajtgj [url=http://www.guccisprings.com/]gucci 財布[/url] dlnyzxv vqzohzp [url=http://www.gucciiget.com/]グッチ バッグ[/url] kmvxqsm hqrebys http://www.lovelovegucci.com/ グッチ 財布 メンズ lmmiedq ndsilgn http://www.gucciiget.com/ グッチ 財布 kuvdvit unaowil [url=http://www.gucciiget.com/]グッチ メンズ[/url] hqgwhlv ghuegjy [url=http://www.lovelovegucci.com/]グッチ 財布[/url] bkxirdq zgwqpse [url=http://www.gucciiget.com/]グッチ アウトレット[/url] aruznhl rdoeirf [url=http://www.guccisprings.com/]グッチ 財布[/url] jbzfwkt rygrved http://www.guccisprings.com/ グッチ 財布 gwshkjg pouorbq [url=http://www.lovelovegucci.com/]グッチ アウトレット[/url] ietvglp lwmwgiz [url=http://www.lovelovegucci.com/]グッチ 財布 メンズ[/url] tkuvwep kfwkbuo [url=http://www.gucciiget.com/]グッチ 財布[/url] pwkaiok smmklce [url=http://www.lovelovegucci.com/]グッチ バッグ[/url] tnxvdiw irhsfxi [url=http://www.guccisprings.com/]グッチ バッグ[/url] jxywkvk vqrdpjl [url=http://www.chloefind.com/]クロエ アウトレット[/url] qsdixq bvsakv [url=http://www.chloefind.com/]クロエ バッグ[/url] bfxfix vqmtyb [url=http://www.chloefind.com/]クロエ 財布[/url] drjdjl qhftzi http://www.chloefind.com/ iguoev lkgyvbx [url=http://www.chloe2013ss.com/]クロエ 財布[/url] cjpdsh jtfnpk [url=http://www.chloe2013ss.com/]クロエ バッグ[/url] hzcbir sdmvbd [url=http://www.chloe2013ss.com/]クロエ 財布新作[/url] jdgpdm sepyup http://www.chloe2013ss.com/ npbdzs
lifpnam http://www.gucciiget.com/ グッチ 財布 pchywpq
xiozvyw http://www.lovelovegucci.com/ グッチ 財布 vmbybco
rxnawfy http://www.guccisprings.com/ グッチ 財布 メンズ audyvgy

Anonymous said...

Thank you for shаrіng your thoughts.
I trulу аppreciate your efforts and Ι wіll be ωaiting
for yоur further post thаnk you onсe again.


Нerе is my раge free disabled dating sites

Anonymous said...

Right here is thе рerfeсt blog for еveryοne who rеally wants to find out about this
topіc. You unԁerstanԁ a whole lot its
almoѕt hаrd to argue with you (not thаt I rеally will nеed to…HaHa).
Yοu certainly put a fresh spin οn а topic thаt's been written about for decades. Great stuff, just great!

Here is my homepage - www.Ihkili.com

Anonymous said...

Terrific article! That is the type of information that should be shared around
the web. Shame on the seek engines for not positioning this publish higher!

Come on over and discuss with my site . Thanks =)

My web blog ... Clicking Here

Anonymous said...

Post writing is also a fun, if you be familiar with afterward you can write otherwise it
is difficult to write.

Here is my weblog: 財布トリーバーチ

Anonymous said...

Fantastic website you have here but I was wanting
to know if you knew of any user discussion forums that cover the same topics talked about here?
I'd really love to be a part of group where I can get comments from other experienced individuals that share the same interest. If you have any recommendations, please let me know. Cheers!

Stop by my page http://maxi-dream.com/

Anonymous said...

Greetings from Colorado! I'm bored at work so I decided to browse your site on my iphone during lunch break. I love the knowledge you present here and can't
wait to take a look when I get home. I'm surprised at how quick your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .

. Anyhow, awesome blog!

Also visit my blog post :: ミュウミュウ店舗

Anonymous said...

Just desire to say your article is as surprising.
The clarity in your post is simply great and i could assume you're an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please continue the enjoyable work.

Here is my homepage :: クロエ ハンドバッグ

Anonymous said...

I was curious if you ever considered changing the structure of your
blog? Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so
people could connect with it better. Youve got an awful lot of text for only having 1 or two images.
Maybe you could space it out better?

Also visit my website - chloe バッグ

Anonymous said...

always i used to read smaller posts that as well clear their motive, and that is
also happening with this paragraph which I am reading here.


my web-site www.toryburchoutletshopx.com

Anonymous said...

Thankfulness to my father who informed me about this weblog, this webpage is truly remarkable.


My blog post; Buy Green Coffee Cleanse

Anonymous said...

It's actually a great and helpful piece of info. I'm happy
that you simply shared this helpful information with us.
Please stay us up to date like this. Thanks for sharing.


Also visit my site :: http://www.uk-import-distributors.co.uk

Anonymous said...

I was recommended this web site through my cousin.
I'm not sure whether this put up is written through him as no one else recognize such targeted approximately my trouble. You're incredible!
Thank you!

Here is my web-site; コーチ 財布

Anonymous said...

Hey, I think your blog might be having browser compatibility issues.
When I look at your blog site in Firefox, it looks fine but when
opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up!
Other then that, superb blog!

my blog トリーバーチバッグ

Anonymous said...

This design is incredible! You certainly know how
to keep a reader amused. Between your wit and your videos, I was almost moved
to start my own blog (well, almost...HaHa!) Wonderful job.
I really enjoyed what you had to say, and more than that,
how you presented it. Too cool!

my site :: クロエ ハンドバッグ

Anonymous said...

Ahaa, its pleasant discussion regarding this paragraph at this place at this weblog, I have
read all that, so at this time me also commenting at this place.


Feel free to surf to my web site - ミュウミュウ店舗

Anonymous said...

Ahaa, іts nісe сonverѕation concerning this parаgгaph herе
at this blog, I hаѵe read all thаt, so now
me аlso commеntіng here.

my page ... DiamondLinks.net Review

Anonymous said...

What's up everyone, it's my first pay a visit at this website,
and post is really fruitful in support of me, keep up posting such articles.



Here is my web-site; pieces vintage clothing

Anonymous said...

Jesus christ spammer, give it a rest.

Unknown said...
This comment has been removed by the author.
Unknown said...

I great rant ! I couldn't agree more.
When it comes to numerics
c++ is essentially useless.
Not because its slow, but because
its a general purpose language
for which numerics is an after thought.

Anonymous said...



C++ sucks. It does. It's huge, bloated, has a sharp learning curve, can be overly complex, etc etc etc. And there are some absolute piles of garbage projects developed in C++.

Because of the complexity of the language and the sharp learning curve associated with it, the typical new comers write terribly structured code and use bad practices, or use the language in a way that it is not meant to (or should not) be used. This is not helped by the fact that many tend to have the mentality that they should use a certain part of the language just because it exists, or "because OOP".

A lot of the rambling in your post stems from the same poor mentality: "this is part of the language, therefore I must use it" Followed by "but I don't know how to use it", and concluded with "therefore it's bad".

I interact with scientists and engineers, and use different combination of languages that include Python, Java, C, C++, Pascal, and Fortran. I landed on your page as I was searching the web in hopes of understanding what leads to the utter programming incompetence of the engineers and scientists I interact with and have in the past.

Without exception, in my experience, these are people who naturally _default_ to, and are in love with Fortran, but also, if pressed, "program" in C++, Java, python, C#, etc on the side depending on projects' needs. These are people who, in my opinion, who shouldn't be allowed to touch a computer, yet alone write "programs". I don't have a lot against the Fortran language itself, it's just the typical Fortran users I can't tolerate. It seems to me that the language itself breeds complete, or maybe that people who are technologically incompetent tend to prefer Fortran over any other programming language.

Ok so let's summarize so far: C++ sucks, and that in my experience people who default to Fortran are utter morons.

Now to change the subject a little, I want to point out that your incessant rambling doesn't really point out _why_ C++ sucks. Instead, its a nonsensical rant filled with gibberish, all centered around C++ not having a linear algebra library built-in to the STL (do you really need to further bloat it?), and highlights your bias to a certain language (Fortran in this case), or your misinformed view and overall ignorance. I have highlighted some of the excerpts that particularly stood out below:

> Then you have to figure out how to link to an external library

Of course you have to read the documentation of the library. Do you expect the documentation to be downloaded to your brain after you download the library itself? Or do you expect things to magically work together? You can run into similar issues with even python modules that need to be compiled from source. You will pretty much run into this "problem" with virtually any other library out there.

> And the documentation is probably not that helpful.

In order to "use" Eigen (a library you bring up yourself), literally all you need to do (as mentioned on the very first page of their website -- I just checked) is to include the header files (and add 4-5 characters in your makefile). No need to download dependencies, no
need to "install", build, or "link to an external library" (in the usual sense), no need to compile with a specific compiler, etc etc. In a way, it's a lot like importing a python module that doesn't even need to be built (by itself). In short, if you can't figure out how to use a header-only library like Eigen with _plenty_ of documentation, there is little hope for you.

Anonymous said...



> All this mess because you wanted to try out C++ to do a simple matrix multiplication. In Matlab it was just a few lines. In C++ it's a month-long project.

If it takes you a month to figure out how to compile your code against Eigen, perhaps you should look for a profession in the fast-food industry. No, really. If you're that incompetent, you're probably better off not needing a computer for your daily tasks.

The very first page of Eigen's web page shows you how to do matrix addition in 3 lines of code (excluding boilerplate code). From download of library to compilation of your own code, I imagine it would take the average highschool hobbyist student... maybe 15 minutes to be up and running.

That's it. That by itself pretty much addresses the rest of your whining. No need to know how templates work, no need for pointers, no need for pointer arithmetic, no need for struggling with "linking external libraries", no need to worry about "memory leaks", blah blah blah. But let's continue.

> And before you use that C++ library, are you really sure you won't end up with a memory leak?

Is it possible that the library might be internally leaking? Yes. Even your python script can actually leak too if you link against a native module. Hell, it's _possible_ that your python script itself leaks (unlikely). Virtually _anything_ can leak. Is it likely? Not if the library is well tested and adopted, but it can. Bottom line is, it doesn't make any sense to worry about an internal memory leakage as an end user. Do you ever "worry" that your internet browser may have a memory leak under such and such condition?

Maybe this wouldn't have sounded so silly if you had said "you _must fully_ familiarize yourself with the API of libraries that use raw pointers." (which by the way, Eigen is not one of them -- unless you force it)

> Chapter 4, including the section on "Pointers and the Free Store" [...blah blah blah]
> You have to understand memory management before you write your first line of code.

In what part of the 3 lines of Eigen code that performs matrix addition do you have to know "memory management"? In effect, you need to know about C++'s memory model as much as you need to know about your OS's file system. Yes, pointers are a part of the language. But they are not required for you to know them to use the language. With the right level of abstraction you don't need to use pointers directly. And obviously your use case doesn't even require them.

> Here's the function call
> This is an example of "easy" in C++.

Jesus. That is _not_ a function call. Also that's a C library.

> at a minimum, you need to learn about templates (...)

Absolute nonsense. Templates and TMP are more advanced topics. Eigen _extensively_ uses templates, yet you can still be oblivious of their existence and and still use them.

> (...) you think I'm just some idiot blogger who doesn't know anything about programming

I can't judge you because I don't know you. But given the your ignorant and misinformed incessant gibberish, I have to admit that I am tempted to think so, or at least put you in the same category of the people I frequently interact with and what guided me to this blog post in the first place.

> If you can program in Matlab, you can program in Fortran.

Nonsense. I've also used Matlab quite a bit (it sucks too). It took me a while to pick up Fortran. Even now, it can take me hours to decipher even a small piece of spaghetti garbage code
the typical Fortran user writes and pushes my way.

Let's summarize and wrap this up: Yes, C++ does sucks, but your post does not point out why it sucks. Fortran seems to be an idiot magnet. This blog post is really just another person whining just because they couldn't hack it. Finally, the absolute worst combination is a Fortran user attempting to use C++: it's a recipe for disaster (see e.g. your blog).

mohit said...

how do you fill out a check
This is my passion and i have also a lot of tutorials about this topic and your salon and work is good. Thank you very for share us such kind of good info