Changes between Version 26 and Version 27 of SwiggingHlsvd


Ignore:
Timestamp:
Jul 14, 2009, 5:08:24 PM (10 years ago)
Author:
flip
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SwiggingHlsvd

    v26 v27  
    317317ndiv = hlsvd_wrapper.new_ndiv()
    318318}}}
    319 
     319{{{
     320#!rst
    320321The ``ctypes.byref()`` call is informative. It says that the C interface demands that I pass by reference, but I don't need access to the pointer here in Python. That tells anyone reading this code that I probably don't care what the called function does with the value, hinting that it is input-only. Again, I wouldn't need this at all if the f2c-generated code was tweaked to eliminate the unnecessary pass-by-reference for the input parameters.
    321322
    322323Also note that the code above includes some value assignments, like this line which assigns the value .256 to tstep:
    323 ::
    324 
    325     tstep = ctypes.c_float(.256)
    326 
     324}}}
     325{{{
     326#!python
     327tstep = ctypes.c_float(.256)
     328}}}
    327329
    328330In my ideal Python example above I ignored assigning values to the variables for brevity's sake. And in order to make a fair comparison of SWIG's code to the ideal, I didn't include assignment in that code either. But to fairly compare SWIG and ctypes I need to mention assignment here, because it's simpler with ctypes. The code to assign a value to tstep via the SWIG interface looks like this:
    329 ::
    330 
    331     tstep = hlsvd_wrapper.new_tstep()
    332     hlsvd_wrapper.tstep_assign(tstep, .256)
    333 
    334 
    335 Arrays are also considerably uglier in SWIG. First consider the ctypes version (inputs is a list that I read from a disk file):
    336 ::
    337 
    338     yi = InputFloatArrayType()
    339     for i, f in enumerate(inputs):
    340         yi[i] = inputs[i]
    341 
     331
     332{{{
     333#!python
     334tstep = hlsvd_wrapper.new_tstep()
     335hlsvd_wrapper.tstep_assign(tstep, .256)
     336}}}
     337
     338Arrays are also considerably uglier in SWIG. First consider the ctypes version (in which inputs is a list that I read from a disk file):
     339
     340{{{
     341#!python
     342yi = InputFloatArrayType()
     343for i, f in enumerate(inputs):
     344    yi[i] = inputs[i]
     345}}}
    342346
    343347And now look at the SWIG version:
    344 ::
    345 
    346     yi = hlsvd_wrapper.new_float_array(2048)
    347     for i in range(0, 2048):
    348         hlsvd_wrapper.float_array_setitem(yi, i, inputs[i])
    349 
    350 
     348{{{
     349#!python
     350yi = hlsvd_wrapper.new_float_array(2048)
     351for i in range(0, 2048):
     352    hlsvd_wrapper.float_array_setitem(yi, i, inputs[i])
     353}}}
     354
     355{{{
     356#!rst
    351357Last but not least, note that the code above is 95% of what I needed to use ctypes to call ``hlsvd()``. There's no interface file, no SWIG-ing step, and no compile & link steps. Just the Python standard library, that's it.
    352358
     
    375381| http://www.netlib.org/f2c/f2c.1
    376382
    377 
    378 
    379 
    380 }}}
     383}}}