4

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 !

0

13 Answers 13

23

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++.
9

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.

4
  • I checked for C++ libs and found that Boost has a support for graphs. Besides being faster to develop, python gives the flexibility to test the quick code snippets and syntax makes it very easy to do it quickly (no includes, no namespaces etc. ) just write anything and HIT ENTER. I am planning to use "python-graph" from google codes ? any suggestion ? Thanks for your time | was definitely helpful :-) Commented Dec 1, 2009 at 2:16
  • I have a fair bit of experience with NetworkX and gladly recommend it. I agree that the Python interactive interpreter (and offshoots like IPython and bpython) is a really wonderful tool, and I miss when working with compiled languages.
    – gotgenes
    Commented Dec 1, 2009 at 2:25
  • writing your own graph lib may be a requirement, though, in an algorithms or data structures class
    – warren
    Commented Dec 1, 2009 at 8:15
  • @warren I already wrote the caveat, "...unless this is an academic excercise..."
    – gotgenes
    Commented Dec 1, 2009 at 14:18
8

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

1
  • it has been only 34 seconds I posted the question, StackOverFLow totally \m/, Thanks John ! Commented Dec 1, 2009 at 1:12
6

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.

6

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.

1
  • Hey Mac, Sorry. I meant faster in sense of "development time" - editing it. Thank you for your time. Commented Dec 1, 2009 at 1:21
6

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

4

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()
4

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.

3

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.

0
2

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.

2

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

1

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

0

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

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.