Using Python for programming algorithms

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vicent Giner

    Using Python for programming algorithms

    Hello.

    I am new to Python. It seems a very interesting language to me. Its
    simplicity is very attractive.

    However, it is usually said that Python is not a compiled but
    interpreted programming language —I mean, it is not like C, in that
    sense.

    I am working on my PhD Thesis, which is about Operations Research,
    heuristic algorithms, etc., and I am considering the possibility of
    programming all my algorithms in Python.

    The usual alternative is C, but I like Python more.

    The main drawbacks I see to using Python are these:

    * As far as I understand, the fact that Python is not a compiled
    language makes it slower than C, when performing huge amounts of
    computations within an algorithm or program.

    * I don't know how likely it is to find libraries in Python related to
    my research field.

    * I know Python is a "serious" and mature programming language, of
    course. But I do not know if it is seen as "just funny" in a research
    context. Is Python considered as a good programming language for
    implementing Operations Research algorithms, such as heuristics and
    other soft-computing algorithms?

    Maybe this is not the right forum, but maybe you can give me some
    hints or tips...

    Thank you in advance.
  • Colin J. Williams

    #2
    Re: Using Python for programming algorithms

    Vicent Giner wrote:
    Hello.
    >
    I am new to Python. It seems a very interesting language to me. Its
    simplicity is very attractive.
    >
    However, it is usually said that Python is not a compiled but
    interpreted programming language —I mean, it is not like C, in that
    sense.
    >
    I am working on my PhD Thesis, which is about Operations Research,
    heuristic algorithms, etc., and I am considering the possibility of
    programming all my algorithms in Python.
    >
    The usual alternative is C, but I like Python more.
    >
    The main drawbacks I see to using Python are these:
    >
    * As far as I understand, the fact that Python is not a compiled
    language makes it slower than C, when performing huge amounts of
    computations within an algorithm or program.
    The usual answer is that development
    time is more important than running time.

    Since you are likely to be using arrays,
    you might look at numpy, where the
    number crunching is using compiled C code.
    >
    * I don't know how likely it is to find libraries in Python related to
    my research field.
    >
    * I know Python is a "serious" and mature programming language, of
    course. But I do not know if it is seen as "just funny" in a research
    context. Is Python considered as a good programming language for
    implementing Operations Research algorithms, such as heuristics and
    other soft-computing algorithms?
    Try Google with Python and your area of
    interest. You could well find
    Python-based packages which meet your needs.
    >
    Maybe this is not the right forum, but maybe you can give me some
    hints or tips...
    >
    Thank you in advance.
    Good luck.

    Colin W.

    Comment

    • castironpi

      #3
      Re: Using Python for programming algorithms

      On May 17, 5:32 pm, Vicent Giner <vgi...@gmail.c omwrote:
      Hello.
      >
      I am new to Python. It seems a very interesting language to me. Its
      simplicity is very attractive.
      >
      However, it is usually said that Python is not a compiled but
      interpreted programming language —I mean, it is not like C, in that
      sense.
      >
      I am working on my PhD Thesis, which is about Operations Research,
      heuristic algorithms, etc., and I am considering the possibility of
      programming all my algorithms in Python.
      >
      The usual alternative is C, but I like Python more.
      >
      The main drawbacks I see to using Python are these:
      >
      * As far as I understand, the fact that Python is not a compiled
      language makes it slower than C, when performing huge amounts of
      computations within an algorithm or program.
      >
      * I don't know how likely it is to find libraries in Python related to
      my research field.
      >
      * I know Python is a "serious" and mature programming language, of
      course. But I do not know if it is seen as "just funny" in a research
      context. Is Python considered as a good programming language for
      implementing Operations Research algorithms, such as heuristics and
      other soft-computing algorithms?
      >
      Maybe this is not the right forum, but maybe you can give me some
      hints or tips...
      >
      Thank you in advance.
      You're hearing from 'impossible and useless'-- neither operations.

      'Stacks' are pretty important to Python (there is a Stackless Python,
      I understand), which makes persistence a little more handy. It's
      still a computer and still a language. You may be asking how well its
      best speakers know, and look at that, I can't tell you. Some of the
      fundamentals of Python may be unexplored to date, as its from the 90s,
      and stacks are elements.

      I, for one, will assume you're interested in control operations, which
      yes Python has, and control is important. The standard library is a
      good size to me (I wouldn't go doubling). There's a ready graphics
      module. There are code-primitive literals, including lists -and- a
      tuple. I think you're looking for the selling points of dynamic
      assignment (a.barproperty= 'unheardof'), typefreeness (a= [2,'bcd']),
      dynamic execution (exec('print 2'), which promotes a possibility of
      self-referentiality) , type-aware function pointers, variable-length
      procedure arguments, and platform independence.

      I think you just asked at the right time. Yes that's an impressive
      list.

      There is one catch to Python, of the importance of which of the powers
      that be, I am unaware. But I do know what you are liable to find on
      the newsgroup. Now, with thousands of dollars of institution time on
      the money, what control? I will be tentatively assuming that you are
      not covertly comparing other languages. I don't think you'll like it
      if you're unwise.

      Comment

      • sturlamolden

        #4
        Re: Using Python for programming algorithms

        On May 18, 12:32 am, Vicent Giner <vgi...@gmail.c omwrote:
        * As far as I understand, the fact that Python is not a compiled
        language makes it slower than C, when performing huge amounts of
        computations within an algorithm or program.
        First of all: whatever you do, use NumPy for all numerical work (and
        possibly Scipy). Remember that Python with NumPy tend to be faster
        than Matlab. Anything that applies to Matlab regarding vectorization
        for speed also applies to NumPy.

        If your program runs too slowly, try to use the psyco jit compiler
        first. If that doesn't help, try one of the following:

        - use Cython or Pyrex and compile your (modified) Python code
        - inline C++ using scipy.weave
        - write a function in C and call it using ctypes
        - write a function in Fortran and make it callable from Python using
        f2py

        Usually, only small bottlenecks matter when it comes to overall
        performance. It is also notoriously difficult to guess where they are.
        Therefore: write everything in Python first, then profile your code to
        identify bottlenecks. Only important bottlenecks need to be translated
        to Pyrex, C or Fortran.



        * I know Python is a "serious" and mature programming language, of
        course. But I do not know if it is seen as "just funny" in a research
        context.
        Google NumPy, SciPy, Matplolib and Sage.

        NASA uses Python to process image data from the Hubble telescope.




        Thank you in advance.

        Comment

        • Henrique Dante de Almeida

          #5
          Re: Using Python for programming algorithms

          On May 17, 7:32 pm, Vicent Giner <vgi...@gmail.c omwrote:
          Hello.
          >
          I am new to Python. It seems a very interesting language to me. Its
          simplicity is very attractive.
          >
          However, it is usually said that Python is not a compiled but
          interpreted programming language —I mean, it is not like C, in that
          sense.
          >
          I am working on my PhD Thesis, which is about Operations Research,
          heuristic algorithms, etc., and I am considering the possibility of
          programming all my algorithms in Python.
          >
          The usual alternative is C, but I like Python more.
          >
          The main drawbacks I see to using Python are these:
          >
          * As far as I understand, the fact that Python is not a compiled
          language makes it slower than C, when performing huge amounts of
          computations within an algorithm or program.
          >
          * I don't know how likely it is to find libraries in Python related to
          my research field.
          >
          * I know Python is a "serious" and mature programming language, of
          course. But I do not know if it is seen as "just funny" in a research
          context. Is Python considered as a good programming language for
          implementing Operations Research algorithms, such as heuristics and
          other soft-computing algorithms?
          >
          Maybe this is not the right forum, but maybe you can give me some
          hints or tips...
          >
          Thank you in advance.
          I guess that python is not a good language for that. Algorithms
          implemented in plain python are many times slower than C ones
          (hundreds ?). In practice, algorithms are written in C and wrapped in
          python. I have near zero experience in operations research, but once I
          looked for linear programming toolkits for python and IIRC, I only
          could find a trivial wrapper for glpk (called pulp).

          My opinion: choose compiled or byte compiled languages. Choose the
          language paradigm that best suit the algorithms.

          Comment

          • inhahe

            #6
            Re: Using Python for programming algorithms

            what little I know:

            The numbers I heard are that Python is 10-100 times slower than C. So use
            Python if you can wait 10-100 times longer. Although it won't really be
            that slow using numpy and/or psyco.

            Python seems to have a really extensive reportoire of modules available for
            it. Although I don't know about things in the math field. If what you want
            is obscure enough, then you might find it for C/C++ and not Python. You
            might find out in a few seconds by googling.

            The advantage to Python (other than production time), is that it would be a
            lot simpler and more readable simply as a syntax for conveying algorithms.
            It would be like pseudo-code... but runnable. ;)





            Comment

            • Lie

              #7
              Re: Using Python for programming algorithms

              On May 18, 5:32 am, Vicent Giner <vgi...@gmail.c omwrote:
              Hello.
              >
              I am new to Python. It seems a very interesting language to me. Its
              simplicity is very attractive.
              >
              However, it is usually said that Python is not a compiled but
              interpreted programming language —I mean, it is not like C, in that
              sense.
              >
              I am working on my PhD Thesis, which is about Operations Research,
              heuristic algorithms, etc., and I am considering the possibility of
              programming all my algorithms in Python.
              >
              The usual alternative is C, but I like Python more.
              >
              The main drawbacks I see to using Python are these:
              >
              * As far as I understand, the fact that Python is not a compiled
              language makes it slower than C, when performing huge amounts of
              computations within an algorithm or program.
              It is slower than a goodly written C code. C codes might be slower
              than Python if the writer of C isn't very familiar with C, on the
              other hand Python's implementation of things like list, dictionary,
              sets, etc are a very optimized piece of code (some says it's the most
              fine tuned implementation in the world) and a sloppily written array/
              dict/sets implementation in C might be slower than Python.
              * I don't know how likely it is to find libraries in Python related to
              my research field.
              >
              * I know Python is a "serious" and mature programming language, of
              course. But I do not know if it is seen as "just funny" in a research
              context. Is Python considered as a good programming language for
              implementing Operations Research algorithms, such as heuristics and
              other soft-computing algorithms?
              Why not ask your peers? AFAIK (since I'm not a researcher, just a high-
              school student with a "too much time" to program, I'm not even
              familiar with Operation Research), in a Operation Research context (as
              I understand it from wikipedia) the important thing is the algorithm
              instead of pure speed, and codes are written just to describe the
              algorithm you're devising. So any programming language that isn't an
              esoteric language (like Whitespace or Brainfuck) would not be
              considered a "just funny".
              Maybe this is not the right forum, but maybe you can give me some
              hints or tips...
              >
              Thank you in advance.

              On May 18, 11:37 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
              On Sat, 17 May 2008 20:25:18 -0700 (PDT), Henrique Dante de Almeida
              <hda...@gmail.c omdeclaimed the following in comp.lang.pytho n:
               My opinion: choose compiled or byte compiled languages. Choose the
              language paradigm that best suit the algorithms.
              >
                      Python IS byte compiled -- that's what .pyc and .pyo filescontain
              (strangely though, one must specify a command line argument to get the
              "optimized" .pyo, rather than the .pyc with debugging data embedded).
              Just to note, currently, "optimized" python code (using -O or -OO)
              does very little to make your code goes faster as it doesn't (yet) do
              anything but removing docstrings. The -O(ptimized) flags is reserved
              to make it possible to add optimizations that might make some features
              unavailable or behave differently (such as the docstrings removal that
              breaks codes that uses __doc__).

              Comment

              • dmitrey

                #8
                Re: Using Python for programming algorithms

                Along with numpy & scipy there is some more Python scientific soft
                worse to be mentioned:




                On 18 ôÒÁ, 06:25, Henrique Dante de Almeida <hda...@gmail.c omwrote:
                once I
                looked for linear programming toolkits for python and IIRC, I only
                could find a trivial wrapper for glpk (called pulp).
                You could be interested in OpenOpt, it has connections to LP solvers
                glpk, cvxopt's one and lp_solve (former & latter allows handling MILP
                as well)


                Comment

                • sturlamolden

                  #9
                  Re: Using Python for programming algorithms

                  On May 18, 5:46 am, "inhahe" <inh...@gmail.c omwrote:
                  The numbers I heard are that Python is 10-100 times slower than C.
                  Only true if you use Python as if it was a dialect of Visual Basic. If
                  you use the right tool, like NumPy, Python can be fast enough. Also
                  note that Python is not slower than any other language (including C)
                  if the code is i/o bound. As it turns out, most code is i/o bound,
                  even many scientific programs.

                  In scientific research, CPU time is cheap and time spent programming
                  is expensive. Instead of optimizing code that runs too slowly, it is
                  often less expensive to use fancier hardware, like parallell
                  computers. For Python, we e.g. have mpi4py which gives us access to
                  MPI. It can be a good advice to write scientific software
                  parallelizable from the start.

                  I learned Pascal my first year in college. When I started programming
                  Matlab, I brought with me every habits of a novice Pascal programmer.
                  Needless to say, my programs ran excruciatingly slow. I learned C just
                  to write faster "mex" extensions for Matlab. But eventually, my skills
                  improved and I found that my Matlab programs did not need C anymore.
                  It took me almost 3 years to unlearn the bad habits I had acquired
                  while programming Pascal. It is very easy to blame the language, when
                  in fact it is the programmer who is not using it properly.
















                  Comment

                  • David C. Ullrich

                    #10
                    Re: Using Python for programming algorithms

                    On Sat, 17 May 2008 15:32:29 -0700 (PDT), Vicent Giner
                    <vginer@gmail.c omwrote:
                    >Hello.
                    >
                    >I am new to Python. It seems a very interesting language to me. Its
                    >simplicity is very attractive.
                    >
                    >However, it is usually said that Python is not a compiled but
                    >interpreted programming language —I mean, it is not like C, in that
                    >sense.
                    >
                    >I am working on my PhD Thesis, which is about Operations Research,
                    >heuristic algorithms, etc., and I am considering the possibility of
                    >programming all my algorithms in Python.
                    Other people have said things about how to use Python
                    effieiently. Something that seems relevant that I don't
                    see mentioned:

                    Are you going to be doing research _about_ the
                    algorithms in question or is it going to be research
                    _using_ these algorithms to draw conclusions
                    about other things?

                    Most of the replies seem to be assuming the latter.
                    If it's the former then Python seems like definitely
                    an excellent choice - when you have want to try
                    something new it will be much faster trying it
                    out in Python, when you write up the results
                    there will be no need for pseudo-code as a
                    guide to the real code because the Python
                    will be just about as easy to read as the
                    pseudo code would be, etc.
                    >The usual alternative is C, but I like Python more.
                    >
                    >The main drawbacks I see to using Python are these:
                    >
                    >* As far as I understand, the fact that Python is not a compiled
                    >language makes it slower than C, when performing huge amounts of
                    >computations within an algorithm or program.
                    >
                    >* I don't know how likely it is to find libraries in Python related to
                    >my research field.
                    >
                    >* I know Python is a "serious" and mature programming language, of
                    >course. But I do not know if it is seen as "just funny" in a research
                    >context. Is Python considered as a good programming language for
                    >implementing Operations Research algorithms, such as heuristics and
                    >other soft-computing algorithms?
                    >
                    >Maybe this is not the right forum, but maybe you can give me some
                    >hints or tips...
                    >
                    >Thank you in advance.
                    David C. Ullrich

                    Comment

                    • sturlamolden

                      #11
                      Re: Using Python for programming algorithms

                      On May 18, 4:20 pm, David C. Ullrich <dullr...@spryn et.comwrote:
                      Are you going to be doing research _about_ the
                      algorithms in question or is it going to be research
                      _using_ these algorithms to draw conclusions
                      about other things?
                      >
                      Most of the replies seem to be assuming the latter.
                      If it's the former then Python seems like definitely
                      an excellent choice - when you have want to try
                      something new it will be much faster trying it
                      out in Python,
                      I second this. Hence my previous statement that "In scientific
                      research, CPU time is cheap and time spent programming is expensive."
                      If it was not clear what I meant, your post can serve as a
                      clarification. But whether Giner is 'developing' or 'using'
                      algorithms, he should value his own labour more than the CPU's. CPU
                      labour (i.e. computation) is very cheap. Manual labour (i.e.
                      programming) is very expensive. He may in any case benefit from using
                      Python. Today, the preferred computer language amount scientists is
                      not Fortran77, but various high-level languages like Matlab, S, IDL,
                      Perl and Python.

                      A related question is: How much 'speed' is really needed? If Giner is
                      analyzing datasets using conventional statistics (ANOVA, multiple
                      regression, etc.), when will Python (with NumPy) cease to be
                      sufficient? In my experience, conventional statistics on a dataset of
                      100,000 or 1,000,000 samples can be regarded child's play on a modern
                      desktop computer. One really need HUGE amounts of data before it's
                      worthwhile to use anything else. If one can save a couple of seconds
                      CPU time by spending several hours programming, then the effort is not
                      just futile, it's downright wasteful and silly.


                      Something else that should be mentioned:

                      The complexity of the algorithm (the big-O notation) is much more
                      important for runtime performance than the choice of language. If you
                      can replace a O(N*N) with O(N log N), O(N) or O(1) it is always
                      adviceable to do so. An O(N*N) algorithm implemented in C is never
                      preferred over an O(N) algorithm written in Python. The only time when
                      C is preferred over Python is when N is large, but this is also when
                      O(N*N) is most painful. Pay attention to the algorithm is things are
                      running unbearably slow.

                      Python has highly tuned datatypes like lists, dicts and sets, which a
                      C programmer will have a hard time duplicating. This also applies to
                      built-in algorithms like 'timsort'. qsort in the C standard library or
                      anything a C programmer can whip up within a reasonable amount of time
                      simply doesn't compare. C vs. Python benchmarks that doesn't take this
                      into account will falsely put Python in a bad light.











                      Comment

                      • Jaap Spies

                        #12
                        Re: Using Python for programming algorithms

                        Vicent Giner wrote:
                        Hello.
                        >
                        I am new to Python. It seems a very interesting language to me. Its
                        simplicity is very attractive.
                        >
                        However, it is usually said that Python is not a compiled but
                        interpreted programming language —I mean, it is not like C, in that
                        sense.
                        >
                        I am working on my PhD Thesis, which is about Operations Research,
                        heuristic algorithms, etc., and I am considering the possibility of
                        programming all my algorithms in Python.
                        >
                        The usual alternative is C, but I like Python more.
                        >
                        The main drawbacks I see to using Python are these:
                        >
                        * As far as I understand, the fact that Python is not a compiled
                        language makes it slower than C, when performing huge amounts of
                        computations within an algorithm or program.
                        >
                        * I don't know how likely it is to find libraries in Python related to
                        my research field.
                        >
                        * I know Python is a "serious" and mature programming language, of
                        course. But I do not know if it is seen as "just funny" in a research
                        context. Is Python considered as a good programming language for
                        implementing Operations Research algorithms, such as heuristics and
                        other soft-computing algorithms?
                        >
                        You definitely should take a look at Sage: http://www.sagemath.org/

                        This may offer all you need, based on Python integrating a lot of
                        other programs!

                        Jaap

                        Comment

                        • Bruno Desthuilliers

                          #13
                          Re: Using Python for programming algorithms

                          Henrique Dante de Almeida a écrit :
                          On May 17, 7:32 pm, Vicent Giner <vgi...@gmail.c omwrote:
                          >Hello.
                          >>
                          (snip)
                          >However, it is usually said that Python is not a compiled but
                          >interpreted programming language —I mean, it is not like C, in that
                          >sense.
                          >>
                          (snip)
                          I guess that python is not a good language for that.
                          (snip)
                          My opinion: choose compiled or byte compiled languages.
                          Slightly OT (ie : not talking about computation-heavy alorgithm being
                          better implemented in C then wrapped in Python - this seems quite
                          obvious) but just a couple facts:

                          1/ being interpreted or compiled (for whatever definition of these
                          terms) is not a property of a language, but a property of an
                          implementation of a language.

                          2/ actually, all known Python implementations compile to byte-code.

                          Comment

                          • Bruno Desthuilliers

                            #14
                            Re: Using Python for programming algorithms

                            Vicent Giner a écrit :
                            Hello.
                            >
                            I am new to Python. It seems a very interesting language to me. Its
                            simplicity is very attractive.
                            >
                            However, it is usually said that Python is not a compiled but
                            interpreted programming language
                            cf my answer to you and Henrique on this.

                            I am working on my PhD Thesis, which is about Operations Research,
                            heuristic algorithms, etc., and I am considering the possibility of
                            programming all my algorithms in Python.
                            >
                            The usual alternative is C, but I like Python more.
                            Then use it.
                            The main drawbacks I see to using Python are these:
                            >
                            * As far as I understand, the fact that Python is not a compiled
                            language makes it slower than C, when performing huge amounts of
                            computations within an algorithm or program.
                            In which way is this a problem here ? I thought your thesis was about
                            algorithm, not about implementation optimisation ? And if it's the
                            later, then even C might sometimes be too high level - you should drop
                            to assembly language.
                            * I don't know how likely it is to find libraries in Python related to
                            my research field.
                            I can't tell but you'd be surprised by the quantity of available Python
                            libs.
                            * I know Python is a "serious" and mature programming language, of
                            course. But I do not know if it is seen as "just funny" in a research
                            context. Is Python considered as a good programming language for
                            implementing Operations Research algorithms, such as heuristics and
                            other soft-computing algorithms?
                            Don't know if this answers your question, but it seems that at least
                            some authors consider it a good choice:


                            All code examples in this books are in Python - very badly written
                            Python, alas...
                            Maybe this is not the right forum, but maybe you can give me some
                            hints or tips...
                            Hem... Obviously, most people here will have a little biased, you know ?-)

                            Comment

                            • Tim Golden

                              #15
                              Re: Using Python for programming algorithms

                              Bruno Desthuilliers wrote:
                              2/ actually, all known Python implementations compile to byte-code.
                              In curiosity, did your "actually" mean, in the French sense, "at the moment"
                              or, in the English sense, "in contrast to something stated earlier"? Or maybe both?

                              TJG

                              Comment

                              Working...