Thread: Good example of terribly written code from a researcher

  1. #1
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

    Good example of terribly written code from a researcher

    An economics researcher at UPenn recently published the following paper:
    http://economics.sas.upenn.edu/~jesu..._languages.pdf

    Here are the codes:
    https://github.com/jesusfv/Compariso...ages-Economics

    Looking at the code makes my eyes bleed.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I can't speak for the Fortran; as for the C++, the code is really just C with some C++ tools used.

    I would not say "eye bleedy", but very much not idiomatic.

    *shrug*

    At least the style used, though an uncommon mix, is very consistent.

    Because I haven't Fortran-ed in a long age*, you'd have to provide some sort of overview.

    Soma

    * I also never really learned canonical Fortran.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    For Fortran:

    1) Mixing double/single precision math (5.0 is taken as single-precision in Fortran as opposed to double in C/C++)
    2) ETIME() is a compiler extension and implementation-defined
    3) real(8) is not portable or guaranteed to be double precision, must use SELECTED_REAL_KIND
    4) using list-directed I/O makes Fortran print out all significant figures of floating-point numbers, causing extra I/O
    5) transposing an array that never changes every iteration of the main loop

    I thought the C++ was a dirty hack; you have to specify -Wl,-stack=4000000 on Windows to get it to compile because of the variable-length arrays. Really, both the Fortran and C++ codes ought to be written to dynamically allocate this memory, as it's up to the compiler as to whether or not automatic arrays are on the heap or stack.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I thought the C++ was a dirty hack; you have to specify -Wl,-stack=4000000 on Windows to get it to compile because of the variable-length arrays.
    O_o

    I see what you are saying. You kind of went the other way. You started with "The code says C++ on the label so should be C++ code.". I started with "The code is really just C code regardless the label.". (In other words, I think the C++ should be removed leaving just C99.) If you go that way, the C++ is definitely worse that I suggested.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Either way would work for me, change it all to C or change it all to C++. If C, I'd replace the VLAs with mallocs for aforementioned reasons. gcc is too dumb to recognize that those arrays need to be stored on the heap.

    Guess I'm just picky. In all honesty, I decided to crap all over this because of the published results which are clearly wrong. I believe anyone who tries to say that GCC is faster than Intel or Clang to be entirely wrong. From my experience, (for Fortran vs. C/C++), Fortran is around 5-10% faster for equivalent code, and for compilers, Intel is the fastest, followed by Clang then GCC. Not sure where MSVC comes in terms of speed.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Clang is really faster than GCC? GNU, how could you betray me?

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    For most of my code, Clang is around 10% faster than gcc. Clang has great potential as I find it to be the best in terms of warning generation and it can be as fast as the Intel compiler at times.

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Wait, just to be specific, are we talking about compilation time or the time it takes to run the executable? All I care about is execution time, tbh. And I assumed that was what we were talking about.

    And to actually add to this topic, I saw some academic meshing code that was terribly written. No CamelCase or underscores and all C code written under a C++ compiler, just using a constructor maybe once or twice.

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Execution time*, though Clang is definitely faster than gcc at compilation time as well, by about 15%. I don't really care about compilation time either unless I'm compiling a huge project.

    *I test everything with -O2 because this is the recommended optimization level for most compilers (references: intel compiler docs, gentoo wiki, various other places I've read, google it)

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Picking on researchers... not fair! Their code may suck but that's not what they care about.

    At a research heavy company like mine there is a clear division between research and production code. It's better to free the researchers from the burden of writing solid code so they can focus on producing intellectual output.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    As I said before, I only picked on him because he said Fortran was slower than C/C++, which is wrong.

    Edit: for numerical computation
    Last edited by Epy; 06-18-2014 at 04:47 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You may have glossed over a particularly important bit here
    Quote Originally Posted by A Comparison of Programming Languages in Economics p4
    Second, to make the comparison as unbiased as
    possible, we coded the same algorithm in each language without adapting it to the pecu-
    liarities of each language (which could reflect more about our knowledge of each language
    than of its objective virtues).5

    5
    It also means that proposals to improve the coding should be done for all languages at the same time
    (unless there is an obvious problem with one of the languages). The game is not to write the best possible C++
    code, it is to write C++ code that is comparable to, for example, Matlab code in computational complexity.
    We are not interested in speed itself, but on relative speed.
    IMO this makes the whole comparison pretty silly. They use (as closely as possible) the exact same implementation in each language. They make little to no effort to use a somewhat optimal or idiomatic implementation in any particular language, or leverage any language features that could seriously shift the results. They basically hold everybody to the lowest common denominator. On one hand, it's "fair", on the other it provides little useful information. It's like comparing a jeep, street bike and minivan by simply looking at the 0-60mph speeds. Sure, the street bike can do it fastest, but it can't transport a family of 5 nor can it handle dirt roads or stream crossings very well.

  13. #13
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by Epy View Post
    Good example of terribly written code from a researcher.
    Looking at the code makes my eyes bleed.
    I do not know Fortran but the Java code looks pretty decent compared to code that I have seen in "enterprise" applications. So heavily patched for break-fixes, customized, gone through hundreds of "coders" who do not know what the original author intended to do. The end version(v1001.11) is delicious spaghetti code ready to kill your sleep and weekend plans. Either I am missing something or you and I have very different definitions for the word "terrible".
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  14. #14
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by stevesmithx View Post
    I do not know Fortran but the Java code looks pretty decent compared to code that I have seen in "enterprise" applications. So heavily patched for break-fixes, customized, gone through hundreds of "coders" who do not know what the original author intended to do. The end version(v1001.11) is delicious spaghetti code ready to kill your sleep and weekend plans. Either I am missing something or you and I have very different definitions for the word "terrible".
    Didn't look at the Java code, only looked at the C++ and Fortran codes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code written in Dev-C++ won't compile on linux
    By FernandoBasso in forum C++ Programming
    Replies: 14
    Last Post: 11-25-2011, 01:56 PM
  2. how would you have written this code?
    By pietrolunz in forum C Programming
    Replies: 11
    Last Post: 08-23-2011, 03:35 PM
  3. Explain following code,doubt written in comments
    By ackr1201 in forum C Programming
    Replies: 9
    Last Post: 07-08-2011, 04:17 PM
  4. Replies: 12
    Last Post: 09-08-2009, 10:16 AM
  5. Re-written code needs help
    By crag2804 in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:43 AM