Performance Issue

I have a python module that is taking in a volume and outputting a new volume. There’s millions of operations that are being done, which I think is the root cause of the performance issue I am having. Furthermore, when I run the script outside of the slicer application (using the terminal), it takes several minutes for the script to complete. I just wanted to see if there are any suggestions or options that I might have for speeding up the performance of the Slicer module. Because the python script on its own is taking several minutes, is that the key rate-limiting issue? Is there a way to do all of those operations more quickly? Could it help improve performance if I use C++ instead of python? Thank you.

Many operations on volumes can be delegated to existing compiled code, either with numpy, VTK, or SimpleITK. So it all depends on what you are calculating in your python code. If there’s no direct implementation with existing filters/operations then sometimes yes, you need to write C++ code. It’s not the end of the world but there are more steps to deal with (see documentation on writing Loadable to CLI modules).

  1. Do you know how significant of an improvement using C++ could be? It would have be extremely significant for it to be worth it to use instead of python
  2. Following up on your comment, if I wanted to edit voxels individually what would be my best bet then? C++? Are there any efficient ways with those libraries you mentioned to do so? I’m not super familiar with VTK or SimpleITK and not sure of an easy way to do that without iterating through the voxels.

Here’s a video that demonstrates the difference. First half is python and second is the same operation in c++.

This video was made with a slightly experimental approach but should be representative of the speed tradeoffs for some operations where you iterate through large data with python code.

1 Like

Iterating through all the voxels of a volume and doing some processing operation on it is typically about 100x faster in C++. This does not mean that you need to use C++, just that you should never ever think about doing pixel-by-pixel processing in Python.

Instead, you can process volumes very efficiently by vector and matrix operations (for example, using numpy) or use other high-level processing functions (in ITK, VTK, SciPy, etc). These tools are all implemented in C++ with a very thin wrapping layer, so they work as fast as raw C++.

You only need to write C++ code if you need to develop new low-level processing algorithms, but that is very rare.