Prototyping with Python code before compiling

前端 未结 7 1377
囚心锁ツ
囚心锁ツ 2021-01-30 17:56

I have been mulling over writing a peak-fitting library for a while. I know Python fairly well and plan on implementing everything in Python to begin with but envisage that I ma

7条回答
  •  悲&欢浪女
    2021-01-30 18:33

    The best way to plan for an eventual transition to compiled code is to write the performance sensitive portions as a module of simple functions in a functional style (stateless and without side effects), which accept and return basic data types.

    This will provide a one-to-one mapping from your Python prototype code to the eventual compiled code, and will let you use ctypes easily and avoid a whole bunch of headaches.

    For peak fitting, you'll almost certainly need to use arrays, which will complicate things a little, but is still very doable with ctypes.

    If you really want to use more complicated data structures, or modify the passed arguments, SWIG or Python's standard C-extension interface will let you do what you want, but with some amount of hassle.

    For what you're doing, you may also want to check out NumPy, which might do some of the work you would want to push to C, as well as offering some additional help in moving data back and forth between Python and C.

提交回复
热议问题