I’ve been studying a bit of algorithms and have been looking at sites like SPOJ.pl TopCoder etc. I’ve seen that programmers prefer C or C++ usually for most algorithmic programming contests.
Now I’ve been having some trouble lately. I know both a bit of C and Python and when trying to write a code I seem to prefer Python over C for most algorithms. Everytime I sit down to write a code in C I give up after about 15 minutes because I find it too cumbersome and tend to move over to python. Passing matrices Pointers and so on seem to be useless time wasted that I could actually be utilizing to think about the algorithm itself.
Now I know and have heard from a lot of people that C is a very important language and is the bread and butter of a lot of programmers out there.
What I wanted to know was whether this approach of mine has any drawbacks/consequences/Disadvantages etc.
This is not a Python vs C debate; This a question about how this specific practice of preferring python over C because of the ease of use will affect me or any other programmer/computer Scientist in the long run.
I’d love to hear from people who’ve used these languages in the industry/and or to develop large software/libraries etc.
6
In my experience, when people have excess difficulty coding algorithms in C, it’s often because they are tightly coupling their data structure management with their algorithm instead of creating appropriate abstractions. For example, manually manipulating linked list pointers everywhere instead of making push()
and pop()
functions. They are too accustomed to having those abstractions provided to them.
While this problem is much more evident with lower level abstractions, failure to recognize tight coupling and create appropriate abstractions is a problem at any level. Practicing these skills in C until you can make an algorithm that looks clean and readable will carry over to any language you use.
The other problem I occasionally see among python programmers is difficulty adapting for performance at scale. Granted, performance isn’t usually the primary concern, but the most pythonic way to implement an algorithm for a relatively small data structure can grind your system to a halt when you’re working with gigabytes or more of data. Becoming a good C programmer helps you be more aware of those kinds of issues in any language.
Can you learn those skills in other languages? Sure, but C helps by making it a lot more obvious when you get it wrong.
That being said, I use python for algorithmic programming when I have a choice, even though I’m just as comfortable in C. Python has language features that make it very nice for that kind of programming, and performance differences are usually negligible. I can’t speak to why other programmers who know both would choose C. I imagine a lot of them do it simply to set themselves apart from the crowd.
1
Researchers whose primary interest is not programming prefer higher level languages such as Python, because they can code a solution more readily in such languages than, say, C. Python is particularly well suited to this because it is more “prototyping” oriented, it is “batteries included,” and it integrates with numerical libraries such as NumPy and SciPy.
If a researcher needs better performance, they will typically hand over the algorithm they created in Python to a Software Engineer, who will find ways to optimize it (up to, and including, recoding in C).
3
Bear in mind SPOJ.pl, the ACM competition and all similar competitions are focused on producing working code fast that is going to be thrown away right after the competition. TopCoder does this, but to a smaller extent (code there is at least properly organized at the OO-design level).
However, in the real world of programming almost every shortcut you’d take in algorithmic programming competitions is an anti-pattern. Only if you take this in mind, can you make any sort of comparison. Let’s take your example: passing a multidimensional array between different functions. In a competition environment, the best approach would be to simply declare the array global to save the time figuring the proper call details (e.g. should I pass the size, or can it be determined?). In real-life programming, I’d do the exact opposite.
So, to your question, are there any complex consequences of choosing Python over C for algorithms, I’d say no. If you’re only interested in the algorithm, you will do the same thing in Python and C. Implementing it in a functional language might bring forward some differences, but the algorithm is still the same.
Virtually the only thing you’ve gained by implementing the algorithm in C is more control over the execution and a guarantee you’re using just lower-level abstractions. This isn’t a small thing, since in Python much of the complexity is hidden. But if the problem is not trivial in the higher-level abstractions, then you’ve only possibly lost execution speed, and in most cases, you’re not really trying to make the program as fast as you can, you’re simply learning.
As already suggested, you can always swap a Python implementation with a C implementation if Python turns out to be too slow. But this will happen probably 2-3 times in a huge project, so starting in C might be a waste of time, unless it’s your language of choice (and you’ve indicated it’s not).
3
As a longtime member of TopCoder and an occasional user of SPOJ I can tell you that a major reason for preferring C/C++ over other languages in competitions is its raw speed. When your program execution is timed, there is an enormous pressure to pick the “fastest” language you can get, because it gives you more slack in terms of coding your algorithm. My progression in TC went from Java to C# to C++.
However, this situation is more common in competitions than in day-to-day development: although writing optimal code is universally important, the relative importance of finishing your code as soon as you can and making it as maintainable as possible usually trumps saving a few hundred CPU cycles. If you are more comfortable coding something in Python, it is very often a preferred solution.
Moreover, Python offers high-level capabilities that are not available in C++. Building them out is often very expensive, and sometimes even impossible (for example, consider building reflection or self-modifying code in C++). In cases like that relying on a higher-level language may prove to be an optimal solution as well.
3
Everytime I sit down to write a code in C I give up after about 15 minutes because I find it too cumbersome and tend to move over to python.
This productivity gain is the common reason that C and C++ jobs have decreased substantially.
This a question about how this specific practice of preferring python over C because of the ease of use will affect me or any other programmer/computer Scientist in the long run.
There are two core parts to this. The first is algortihmic programming. It really doesn’t matter what language you use to express the algorithm. Working with the algorithm itself and fitting the right ones into the right problems are the key parts, so there’s no real issue there.
The second part is productivity gains. Using things that make you more productive over time is a good habit, and something that will do nothing but benefit you during your career. Being able to express the algorithms in different languages is very helpful, but that helpfulness sits more about what idioms the languages use not necessarily what those languages are.
In short, don’t worry about it. What you use to express the algorithm is far less important than being able to express it at all.
1
The advantages of using higher level languages like Python or Ruby are that (1) their syntax is very close to pseudocode and (2) their standard libraries provide useful data structures out of the box (the batteries included concept that @Robert mentioned). So it’s perfectly fine to prefer using them. Use whatever maximizes your productivity, instead of picking a language just because it is mainstream or “cool”.
1
What you will be missing out when programming in “higher” level languages than C/C++ is learning how computers work. You will not be able to develop things like embedded systems, operative systems and hardware drivers. Knowing C also helps when learning assembler.
Also, the vast majority of all mission critical systems are still developed in C, so you may not be able to work in several software software branches (aerospace/automotive/med-tech etc) without knowing it.
3
If a question ever goes on about ‘Big O notation’ and you try and measure that, then it can be harder to do in Python unless you know a lot more about how python implements things, for example a Python list is not a linked list; Pythons sort is TimSort; Python garbage collects at certain times…
I always find it easier to connect a C program to what is likely to be happening on a processor, but even here, there is processor caching; time-slicing of the OS; Compiler optimizations etc that can affect my intuition.
I find it quicker to write and debug Python code so, when given a choice, I would write first in Python concentrating on getting something that worked. With this working Python program you can often slot it into a larger system and find out not only that it worked but also if it was fast enough or in what aspect was it slow. Getting some real performance data then helps when you optimize for speed and allows you to test the Python version against any later rewrites in Python or C or whatever.
So drawbacks to using just Python is that it can be difficult to reap the benefits of algorithms that were written expecting some C-like compilation to processor model.
Drawbacks to using just C are as you have stated: Its a pig to write and debug and you end up having to write your own libraries too often.
I think it would be best to use them both (and other languages), until you get a feel for their trade-offs. I myself was a good C coder but now write very little original C code, although I still have to read (and sometimes debug) C code in my work. Although I prefer Python, I know and still use Perl and Awk (and sed and grep and sort and Tcl and C and …).
1
I would advice you looking at Scala or Clojure (but use type annotations). In some cases they can be as fast as C, in other cases they are still much more faster then Ruby/Python, while having very consice and clear notation unlike C (IMHO). Consider this vs C code:
for (i <- 1 to 100; j <- 2 until 100;
k <- 1 to 2; if i != j) {
//...
}
Also they have function programming arsenal similar to Ruby’s/Python’s map
, filter
, reduce
etc which is not as fast as iterating or tail call recursion, however it is still much faster then the fully dynamic scripting languages.
0
I’d love to hear from people who’ve used these languages in the industry/and or to develop large software/libraries etc.
I’ve worked on a small part of a big C++ library for some years, and have written both my bachelor and master thesis in the context of this library. The library, incidentally, is a library for bioinformatics algorithms and data structures.
The library is built in C++ because C++ is almost perfect for the specific requirements of this library, and for algorithms libraries in general. If I were to develop another algorithms library and the choice of language were mine, I would almost certainly choose C++ again.
The reason is not only performance but also the strong type system which first of all gives you more type safety and second of all gives you the ability to let your types document the algorithm used. This can (in my experience) greatly enhance readability and maintainability.
That said, for simple algorithmic doodles and puzzles, I almost always use Python (mainly because yes, it reads almost like pseudo code), unless I specifically want to try out how to best formulate a problem in C++. So far, I haven’t solved many of the SPOJ or TopCoder problems so I don’t know whether performance there is really so critical that using a fast language is crucial.
But normally what counts is to get the algorithm right in order to pass. In those cases, Python works just fine. For instance, for the Project Euler problems (which aren’t timed, only the correct solution counts), Python is perfectly well suited.