Changes between Version 26 and Version 27 of SwiggingHlsvd
- Timestamp:
- Jul 14, 2009, 5:08:24 PM (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
SwiggingHlsvd
v26 v27 317 317 ndiv = hlsvd_wrapper.new_ndiv() 318 318 }}} 319 319 {{{ 320 #!rst 320 321 The ``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. 321 322 322 323 Also 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 327 tstep = ctypes.c_float(.256) 328 }}} 327 329 328 330 In 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 334 tstep = hlsvd_wrapper.new_tstep() 335 hlsvd_wrapper.tstep_assign(tstep, .256) 336 }}} 337 338 Arrays 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 342 yi = InputFloatArrayType() 343 for i, f in enumerate(inputs): 344 yi[i] = inputs[i] 345 }}} 342 346 343 347 And 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 350 yi = hlsvd_wrapper.new_float_array(2048) 351 for i in range(0, 2048): 352 hlsvd_wrapper.float_array_setitem(yi, i, inputs[i]) 353 }}} 354 355 {{{ 356 #!rst 351 357 Last 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. 352 358 … … 375 381 | http://www.netlib.org/f2c/f2c.1 376 382 377 378 379 380 }}} 383 }}}