Which is the best language (C ++ or Python) for complex problem solving tasks (e.g. 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 it were done with Python. As it would be much faster to evolve, and less time is spent on programming details that actually do not earn anything a reasonable solution.

EDIT 2 . I plan to use python-graph lib from google codes. Please provide sample codes if you have used it.

EDIT 1 : faster - less time && & less work to encode a solution

Thank you all for your help!

+4
source share
13 answers

I think you are looking for Python because you can:

  • Focus on the algorithms themselves and not have to worry about other details such as memory management.
  • Do more with less code.
  • The syntax is almost like working with pseudocode.
  • There is a lot of built-in language support for lists, tuples, lists, etc.

But more specifically ...

  • If better means development speed , select Python .
  • If at better you mean maximum execution speed , select C ++ .
+23
source

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

You should not write your own graph library if you already have a lot (read, "too many"), both in C ++ and Python . [Qaru has a list of Python Graph packages with recommendations that you should check. I did not find an equivalent question for C ++. You can run it.] In addition, if this is not an academic exercise or research on really new graph algorithms, you should look at the available libraries to see if they really already implement these algorithms of interest to you.

Using libraries mitigates the growth of a higher-level language in Python, since you will not be working with raw C ++ as such, but using the C ++ library. Since Python has a very small kernel, it is fairly easy to learn, but it still takes time. You will need the factor in extra time when you study the library if you choose the Python route, so be sure to weigh this in your solution.

Thus, given that you already know C ++, you may ultimately find it faster in C ++, since you only need to set aside the time needed to learn the library, not the library, as well as the new language, and also easy to learn language.

+9
source

A bit subjective, but I voted for python because it has good libraries and abstracts a lot of low-level details that you have to consider when using C ++ ...

+8
source

I worked all my algorithms in college in C ++ because I knew this.

If I had to learn the language at the same time, I would choose Python most likely.

+6
source

I get the impression that it really depends on what you mean faster.

Evolve faster: upgrade to python. Launch faster: switch to C ++.

However, python can use many external C libraries, so the difference in processing time may not be as relevant, depending on the type of implementation.

+6
source

At my university, 500 students in the Algorithms and Data class choose the language they want.

Python is by far the most popular choice, and I personally am happy that I chose this too, although I already knew C ++.

+5
source

Agree with your friend - use Python and put them in the unit test framework.

I have been working for several years with scientists who have worked a lot on their algorithmic work in Python.

The following example shows typical tests (the import statement for the material being tested), with a few subtleties that can save you some time.

The business of saving and restoring sys.path is that all of your tests can be in the directory located next to the src directory, without having to install the source in the main Python modules.

This test script is written in such a way that it can be imported into a wider range 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() 
+4
source

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

+4
source

The algorithms are great in Python (although you can only fly one scheduled python OS thread due to global blocking); however, when it comes to data structures + algorithms, you need fixed guarantees of complexity, in which case you mix Python with C.

I believe that what I said is more about lengthy computing. You can emulate data structures on top of the python hashmap primitive.

+3
source

I will also vote for python. When we execute an algorithm, we usually work on the algorithm itself, and not the language, low-level details. Basically, we are working on the level of abstraction. And using python, we are less likely to track sides. But if you are very familiar with C ++ and are free to use it to express your idea, just use it.

+2
source

With C ++, you sometimes concentrate more on language issues than the problem itself, which is why Python. I would even recommend that you do this in a higher level language such as Matlab (although the language itself may be a little ugly).

+2
source

If using C ++ means that STL is fair play, I would say that it deserves serious consideration. STL is a fantastic library that combines structures, iterators and algorithms. I like Python recommendations, but if I could use STL, I would revise C ++.

+1
source

Remember that Python is compiled into bytecode and then interpreted in a virtual machine. Thus, performance is not better (faster) than C ++.

0
source

All Articles