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.

  • this topic won't be complete without link to this discussion lukeplant.me.uk/blog/posts/…
    – permeakra
    Commented Aug 8, 2012 at 19:27
  • 11
    @permeakra: That's just a rant, basically stating that learning Haskell and Python doesn't make you better in other languages because those other languages suck. Commented Aug 8, 2012 at 19:33
  • It is not just a rant, as it contains description how Python and Haskell influence mind of their user, and many comments of other peoples on that topic. It does not use c as low-level language in comparison, though, but a a bit more hi-level language, but idea is the same - one starts to bring ideas from other language into one he currently working in, making code non-idiomatic. It may be a good thing, but...
    – permeakra
    Commented Aug 8, 2012 at 20:07
  • 1
    This is my take on it: plus.google.com/101996847784434186165/posts/7941QuL69yP Commented Aug 8, 2012 at 20:30
  • It would have been interesting to see a bit of a non-algorithmic programming, whatever this thing is.
    – SK-logic
    Commented Aug 10, 2012 at 8:39

10 Answers 10


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
    "They are too accustomed to having those abstractions provided to them." Does this assume that I learnt python before C and therefore am unable to adapt or something along those lines?
    – ffledgling
    Commented Mar 8, 2013 at 18:59

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).

  • So basically researchers and Software Engineers have a designer Crafts-man relationship? And What about the utilization of both types of people in the industry?
    – ffledgling
    Commented Aug 8, 2012 at 19:16
  • 9
    I think the takeaway is that coding an algorithm in Python, and then writing a more refined implementation in C, is a perfectly acceptable scenario. Commented Aug 8, 2012 at 19:28
  • or "recoding in Cython"?
    – endolith
    Commented Oct 24, 2014 at 2:24

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).

  • 1
    If one were writing a real math intensive application one would almost certainly choose C or C++ (there is no such thing as 'C/C++') because of the huge performance increase over any interpreted language. I looked at Topcoder a couple of years ago, and I recall seeing a lot C++ that wantonly leaked memory, since the contests didn't care about minor details like leaks. I wasn't impressed. Commented Aug 8, 2012 at 22:18
  • Precisely my point. It's a question of priorities: topcoders don't care about memory leaks, since the kernel is going to clean after them anyway; they don't care about bad practices nor anti-patterns, if they save time.
    – K.Steff
    Commented Aug 8, 2012 at 22:40
  • 2
    Your last paragraph embodies the golden rule: "make it work, then make it fast". Commented Aug 9, 2012 at 0:11

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.

  • Since you are a user of TC and SPOJ. Is the trade off between time and simplicity very large if we use python for the code? I.e is it possible to make successful submissions using python if the same algorithm can be successfully submitted using C? (Yes I know it will/might vary greatly question to question but will there be a dis-advantage in most cases or just some?)
    – ffledgling
    Commented Aug 8, 2012 at 19:56
  • @Ayos I cannot speak for Python because I never used it in the context of TC or SPOJ, but the advantage of C++ over C# and Java is only occasionally important, and even then it is not overly significant. I can remember only one case when a straightforward port of an algorithm that has been coded in C++ to C# has failed with a timeout, but it was in a practice room. Most of the time, discovering the correct algorithm is the only thing that makes a difference between successful and failed submissions. Commented Aug 8, 2012 at 20:09
  • 1
    Note that interpreted languages like Python, Ruby and Perl run several times slower than compiled high-level languages like Java and C# (which are themselves slow compared to C). Ultimately though, it doesn't really matter unless you plan on working with either exceptionally large data sets, or need real time speed.
    – KChaloux
    Commented Aug 10, 2012 at 12:37

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.

  • 3
    "C and C++ jobs have decreased substantially". Huh? This seems taken out of the blue, as I see the opposite trend. -1 until you can state a source for that statement.
    – user29079
    Commented Aug 10, 2012 at 6:13

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".

  • Are you a hipster or something? Here's your PBR. Me? I'd rather be cool. Commented Aug 8, 2012 at 20:38

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.

  • The question was explicitly about algorithms, not about close-to-metal aspects. Commented Aug 10, 2012 at 10:24
  • @KonradRudolph Well ok, but writing hardware drivers is most often very closely related to algorithms. For example, when writing a driver for an analog-to-digital converter, you will have to develop digital filters and perhaps also some sort of queue or priority system. And then an API on top of your driver. It is very similar to writing an "object" or "abstract data type".
    – user29079
    Commented Aug 10, 2012 at 10:57
  • @Lundin Thanks for mentioning the disadvantages in the real world scenario.
    – ffledgling
    Commented Aug 11, 2012 at 23:46

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 ...).

  • I disagree with the first paragraph. Python has a strong emphasis on data structures and clearly documents how the predefined data structures are implemented. Of course, garbage collection will skew runtimes, but it will rarely skew the big-O order. Commented Aug 10, 2012 at 10:25

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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.