Tutorial :which is a better language (C++ or Python) for complex problem solving exercises (ex. Graphs)?


I am trying to work on some problems and algorithms. I know C++ but a friend told me that it would be better if done with Python.As it would be much faster to develop and less time is spent in programming details which does not actually earn anything solution wise.

EDIT 2: I plan to use python-graph lib from Google-codes, Please provide example codes if you have used it.

EDIT 1: faster - less time && less work to code the solution

Thank you all for your help !


I think you're looking for Python, because you can:

  • Focus on the algorithms themselves and not have to worry about other detail like memory management.
  • Do more with less code
  • The syntax is almost like working with pseudo code.
  • There is great built in language support for lists, tuples, list comprehensions, etc...

But more specifically...

  • If by better you mean speed of development, then chose Python.
  • If by better you mean sheer execution speed, then chose C++.


Instead of focusing on the language, you should focus on the libraries available to you.

You shouldn't write your own graph library when there are already plenty (read, "too many") available, both in C++ and in Python. [Stack Overflow has a list of Python Graph packages with recommendations that you should check out. I didn't find an equivalent question for C++. You could start one.] Also, unless this is an academic excercise or research on truly novel graph algorithms, you should browse through the libraries available to see if any of them already implement those algorithms of interest to you.

Using libraries mitigate the gain of the higher level language of Python, since you won't be working with raw C++ per se, but with a C++ library. Since the Python language has a very small core, it is fairly easy to learn, however, it still takes time. You will need factor in that additional time on top of learning a library, if you choose the Python route, so make sure to weigh that in your decision.

Thus, given you already know C++, you may ultimately find it faster to develop in C++, since you only have to commit the time necessary to learn the library, not a library plus a newâ€"albeit easy to learnâ€"language.


A bit subjective, but I'd vote for python because it has good libraries and abstracts a lot of the low level 'detail' that you'd have to consider when using c++...


I did all my algorithms work in college in C++ because I knew it.

If I'd had to learn a language at the same time, I would have picked Python most likely.


I am under the impression that it really depends from what you mean by faster.

Faster to develop: go python. Faster to run: go C++.

However python can use a lot of external C libraries, so the difference in processing time might not be that relevant, depending on the type of implementation.


At my university the 500 students in the "Algorithms and Datastructures" class get to choose the language they want.

Python is by far the most popular choice there, and personally I'm happy I also chose that, even though I already knew C++.


Agree with your friend - use Python and put them within a unit test framework.

I worked for a number of years with scientists who did a lot of their algorithmic work in Python.

The example below shows typical tests (the import statement is for the stuff being tested), with a couple of niceties that might save you some time.

The business with the save and restore of sys.path is so you can have all your tests sitting in a directory adjacent to the src directory, without having to install the source into your main Python modules.

This test script is written so it can be imported into a larger suite of unit tests or simply run with python thisfile.py.

#!/usr/bin/python  """  VRML Writer tests  """    import unittest  import os  import sys    if __name__ == '__main__':      global save_syspath      save_syspath = sys.path      sys.path = [os.path.abspath("../src/")] + sys.path    from cgtools.VizValueTools import *    if __name__ == '__main__':      sys.path = save_syspath  # RESTORE SYS.PATH    # use some common constants to make tests easier  MINV = 0.002  MAXV = 12.789    class TestColdHotColorGeneration(unittest.TestCase):        def testGeneratesLimitValues(self):          assert generateColdHotColorValue(MINV, MAXV, MINV) == (0.0, 0.0, 1.0)          assert generateColdHotColorValue(MINV, MAXV, MAXV) == (1.0, 0.0, 0.0)          assert generateColdHotColorValue(0, 0, 0) == (1.0, 0.0, 0.0)  # cope with weird case where range is effectively one value, should be always top          def testGeneratesLimitValuesWithClipping(self):          assert generateColdHotColorValue(MINV, MAXV, MINV - 1.2) == (0.0, 0.0, 1.0)          assert generateColdHotColorValue(MINV, MAXV, MAXV + 49) == (1.0, 0.0, 0.0)          def testGeneratesMiddleValue(self):          """          Note to be careful picking values so your value IS in the middle,          to generate pure green          """          assert generateColdHotColorValue(1.0, 3.0, 2.0) == (0.0, 1.0, 0.0)    if __name__ == '__main__':      # When this module is executed from the command-line, run all its tests      unittest.main()  


I would go for python. And if you really need the performance, then you can always write C/C++ extensions and use them in python.


Algorithms are fine in Python (allthough you can only fly one OS scheduled python thread due to the global lock); however, when it comes to data structures + algorithms you need fixed complexity guarantees, and this case you mix Python with C.

I suppose what I have said applies more to long running computations. You can emulate data structures on-top of the python hashmap primitive.


I also vote for python. When do algorithm, we tend to work on the algorithm itself rather than language, low level details. Basically, we works on abstraction level. And using python, we're less likely to be side-tracked. But if you're very familiar and comfortable with C++ and can use it freely to express your idea, just use it.


With C++, you'd sometimes be concentrating more on the language issues than the problem itself, so Python. I'd even be recommending you do it in a higher-level language like Matlab (although the language itself can be a bit ugly).


If using C++ means that STL is fair game, I'd say that it deserves serious consideration. STL is a fantastic library, combining structures, iterators, and algorithms. I love the Python recommendations, but if I could use STL I'd reconsider C++.


Remember that Python is compiled to bytecode and then interpreted in a VM. So, in performance isn't better (faster) than C++.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »