# Is there any difference in performance (in Registration) between python and CSharp in SimpleITK?

**URL:** https://discourse.itk.org/t/is-there-any-difference-in-performance-in-registration-between-python-and-csharp-in-simpleitk/4831
**Category:** Beginner Questions
**Tags:** registration, python, csharp, simpleitk
**Created:** [February 25, 2022, 5:22am UTC](https://discourse.itk.org/t/is-there-any-difference-in-performance-in-registration-between-python-and-csharp-in-simpleitk/4831 "2022-02-25T05:22:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![jpad](https://discourse.itk.org/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jpad](https://discourse.itk.org/u/jpad)
#### Post date: [February 25, 2022, 5:22am UTC](https://discourse.itk.org/t/is-there-any-difference-in-performance-in-registration-between-python-and-csharp-in-simpleitk/4831/1 "2022-02-25T05:22:56Z")

</div>

I use version 2.2 of simpleitk.  
It was implemented by referring to the following code for registration.

[https://simpleitk.readthedocs.io/en/master/link\_ImageRegistrationMethodBSpline2\_docs.html](https://simpleitk.readthedocs.io/en/master/link_ImageRegistrationMethodBSpline2_docs.html)

Previously, image registration was performed using python, and good results were obtained.  
However, this time, I tried to registration with the same parameter and the same image using CSharp, and I got a bad result.

In both cases, The main parts are as follows.

```
transformDomainMeshSize=[10]*moving.GetDimension()
tx = sitk.BSplineTransformInitializer(fixed, transformDomainMeshSize, 3)

R = sitk.ImageRegistrationMethod()
R.SetMetricAsMattesMutualInformation(50)
R.SetOptimizerAsGradientDescentLineSearch(5.0, 100, convergenceMinimumValue=1e-1, convergenceWindowSize=5)
R.SetOptimizerScalesFromPhysicalShift()
R.SetInitialTransform(tx)
R.SetInterpolator(sitk.sitkLinear)

R.SetShrinkFactorsPerLevel([4,2,1])
R.SetSmoothingSigmasPerLevel([4,2,1])

outTx = R.Execute(fixed, moving)

```

\<C#\>

```
        var transformDomainMeshSize = new VectorUInt32();
        transformDomainMeshSize.Add(10);
        transformDomainMeshSize.Add(10);

        var tx = new BSplineTransformInitializerFilter();
        tx.SetTransformDomainMeshSize(transformDomainMeshSize);
        var tx_t = tx.Execute(fixedImage);

        R = new ImageRegistrationMethod();
        R.SetMetricAsMattesMutualInformation(50);
        R.SetOptimizerAsGradientDescentLineSearch(5.0, 100, 1e-1, 5);
        R.SetOptimizerScalesFromPhysicalShift();
        R.SetInitialTransform(tx_t);
        R.SetInterpolator(InterpolatorEnum.sitkLinear);

        VectorUInt32 shrinkFactorsPerLevel = new VectorUInt32(3)
        {
            4,
            2,
            1
        };

        VectorDouble smoothingSigmasPerLevel = new VectorDouble(3)
        {
            4,
            2,
            1
        };

        R.SetShrinkFactorsPerLevel(shrinkFactorsPerLevel);
        R.SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel);

```

For other parts, I think the same function and parameter were used.

Is there any difference in performance between python and CSharp in SimpleITK?  
Or is it a problem with my code?

Please give me advice.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [February 27, 2022, 8:16pm UTC](https://discourse.itk.org/t/is-there-any-difference-in-performance-in-registration-between-python-and-csharp-in-simpleitk/4831/2 "2022-02-27T20:16:45Z")

</div>

Hello @jpad,

Under the hood it is the same C++ code for all the wrapped languages.

One thing that may be different is the `SetMetricSamplingStrategy` setting and the `SetMetricSamplingPercentage ` or `SetMetricSamplingPercentagePerLevel` settings, not provided in the code so guessing here.

If any of these are different, the registration settings are different. Finally, even when all settings are the same, there is built in variability due to the use of a randomization in the registration. For additional details see the [reproducibility section](https://simpleitk.readthedocs.io/en/master/registrationOverview.html#reproducibility) in the registration overview.
