Converting a c array to python list

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

    Converting a c array to python list

    Hi!

    I want to embed a function in my python application, that creates a
    two-dimensional array of integers and passes it as a list (preferably a
    list of lists, but that is not necessary, as the python function knows
    the dimensions of this array). As I read the reference, I see, that I
    must first initialize a list object and then item-by-item put the values
    to the list. Is there any faster way to do it? And is it worth to
    implement? The same problem is resolved in the current version by
    calling a smaller c function (that counts just one element of the array)
    many times. Will it add much performance to the process?

    zefciu
  • Scott David Daniels

    #2
    Re: Converting a c array to python list

    zefciu wrote:
    Hi!
    >
    I want to embed a function in my python application, that creates a
    two-dimensional array of integers and passes it as a list (preferably a
    list of lists, but that is not necessary, as the python function knows
    the dimensions of this array). As I read the reference, I see, that I
    must first initialize a list object and then item-by-item put the values
    to the list. Is there any faster way to do it? And is it worth to
    implement? The same problem is resolved in the current version by
    calling a smaller c function (that counts just one element of the array)
    many times. Will it add much performance to the process?
    >
    zefciu


    This will allow you to provide a python "View" onto your C data.
    The view can be cut into a list of views as on_way or other_way below:
    ...
    view = block.View(...)
    stride = <<one dimension>>
    one_way = [view[n: n + stride] for n in range(0, len(view), stride)]
    other_way = [view[n::stride] for n in range(stride)]
    ...
    One of these will give you row-major, and the other column-major access
    to the live data in the C array. If you don't want to see your program
    data as it changes, you could Create a Block and fill it. If you need
    Python 2.4 or 2.5, you'll need to figure out how to build from sources
    on Windows (I assume building from sources is otherwise "easy").

    --
    --Scott David Daniels
    scott.daniels@a cm.org

    Comment

    • zefciu

      #3
      Re: Converting a c array to python list

      I have just read about buffer and array objects and I think one of them
      could be fit for my need. However there are two questions.

      If i make a buffer from a part of dynamically allocated memory, what
      would free it? Should it be allocated with malloc or some
      python-specific function?

      How on earth can I create array object in C?

      zefciu

      Comment

      • Russell E. Owen

        #4
        Re: Converting a c array to python list

        In article <es66m4$cnr$1@i news.gazeta.pl> ,
        zefciu <zefirek@Speaco ck.Pau.Apoznan. Mplwrote:
        Hi!
        >
        I want to embed a function in my python application, that creates a
        two-dimensional array of integers and passes it as a list (preferably a
        list of lists, but that is not necessary, as the python function knows
        the dimensions of this array). As I read the reference, I see, that I
        must first initialize a list object and then item-by-item put the values
        to the list. Is there any faster way to do it? And is it worth to
        implement? The same problem is resolved in the current version by
        calling a smaller c function (that counts just one element of the array)
        many times. Will it add much performance to the process?
        My first thought is to use the numpy library since it is good at quickly
        creating large arrays (of any dimension) and you can easily get the data
        out as a list if you really need that (but are you sure you need that?
        You may be able to just use the numpy array directly).

        It might help to have a clearer idea of why you want to do this.

        -- Russell

        P.S. numarray or Numeric would also do the job. They are older,
        deprecated numeric libraries. numpy is recommended for new code.

        Comment

        • zefciu

          #5
          Re: Converting a c array to python list

          Russell E. Owen wrote:
          >
          It might help to have a clearer idea of why you want to do this.
          >
          I am writing a Mandelbrot fractal generator with Tkinter interface. Now
          the generation works like this - there is a loop in python which
          iterates through fractal's pixels and for each of them calls a functions
          which checks if it belongs to Mandelbrot set or how many iterations does
          it take for this point to bail out. Then the python function sets the
          pixel's colour according to the result.

          I was testing it first with python function, written rather for
          readibility not speed, using the builtin complex class. It took about
          90 s to generate a 1152x864 fractal. Then I used a c function and it
          took 14 s. When I told it to my friend (grad student of informatics) he
          said "And probably 90% of the time takes calling the function".

          So I think, that maybe I should put the whole loop into c code. But as
          before the function returned only one integer at every call, now it
          would have to return a rather large array of integers (maybe chars would
          be enough).

          zefciu

          Comment

          • zefciu

            #6
            Re: Converting a c array to python list

            Dennis Lee Bieber wrote:
            Written properly, all it returns is the address of that array data
            -- there is no massive copying of data..
            I know :) That's why I want to know how to write it properly.

            zefciu

            Comment

            Working...