# elasitx performance issue with itk \> 4.8

**URL:** https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467
**Category:** Engineering
**Created:** [December 10, 2018, 2:52pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467 "2018-12-10T14:52:11Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![tvanwalsum](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tvanwalsum](https://discourse.itk.org/u/tvanwalsum)
#### Post date: [December 10, 2018, 2:52pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/1 "2018-12-10T14:52:11Z")

</div>

Hi all,

I recently upgraded my elastix registration software, and simultatneously upgrading itk from 4.8 to the 4.13. This greatly affected the registration timings (3 to 4-fold slower). After some testing, I found it this problem starts with itk 4.9. My parameterfiles contains a MultiMetric registration with Bending Energy Penalty and Advanced Mattes Mutual Information. From the elastix logs. it seems that the increased timings are mainly caused by the multi-metric registration (and they are e.g. not present in a single metric mattes mutual information).

Is there any registration expert or itk expert that may have an idea on the cause of this, and how to solve it?

I’m running Windows 10, using VS2013 (CV12), 64 bits, and compile itk and elastix with all default settings.

Best,

Theo

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [December 10, 2018, 3:08pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/2 "2018-12-10T15:08:46Z")

</div>

Hi Theo,

Did you build ITK and elastix in `Release` mode? Visual Studio defaults to `Debug`.

HTH,  
Matt

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 10, 2018, 4:01pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/3 "2018-12-10T16:01:54Z")

</div>

Also it is possible that different versions of Visual Studio optimize code differently. A relatively recent discussion about that can be found [here](https://discourse.itk.org/t/why-resampleimagefilter-is-slow/1217).

---

<div class="post-metadata">

### Author: ![tvanwalsum](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tvanwalsum](https://discourse.itk.org/u/tvanwalsum)
#### Post date: [December 10, 2018, 7:58pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/4 "2018-12-10T19:58:19Z")

</div>

Dear Matt,

Yes, indeed I did compile both in Release mode.

Btw, I tried both with elastix 4.8 and 4.9, as well as the current version of elastix. And similar behavior was on another laptop running Windows 7.

Best regards,

Theo

---

<div class="post-metadata">

### Author: ![tvanwalsum](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tvanwalsum](https://discourse.itk.org/u/tvanwalsum)
#### Post date: [December 10, 2018, 7:59pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/5 "2018-12-10T19:59:28Z")

</div>

Dear Dzenanz,

Thanks for your response. In this case, I compiled all with the same Visual Studio (VS 2013, which is VC12), on the same system.

Best regards,

Theo

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 10, 2018, 8:37pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/6 "2018-12-10T20:37:38Z")

</div>

Can you provide a [minimum working example](http://sscce.org/)? In doing so, you might as well discover what is the source of the performance loss.

---

<div class="post-metadata">

### Author: ![tvanwalsum](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tvanwalsum](https://discourse.itk.org/u/tvanwalsum)
#### Post date: [December 10, 2018, 9:53pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/7 "2018-12-10T21:53:02Z")

</div>

Hi,

I can provide a working example (don’t think it is really minimum), let me know where I can send it.

It demonstrates the elastix call, that does a rigid registration, followed by a non-rigid one. When inspecting the elastix log-files between both runs, the major timing difference is in the non-rigid (the multi-metric registration), mainly in the last column of timings (which, as far as I understand, relates to combining the metric values, i.e. the multi-metric part).  
I can confirm that running in single metric mode (i.e. leaving out the bending energy), there is not much difference between both itk-versions.

Best,

Theo

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 11, 2018, 3:33pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/8 "2018-12-11T15:33:20Z")

</div>

That is the hard part of narrowing down a problem: making a small example that demonstrates the problem.

If you think combining the metric values is the problem, can you modify the code of your big example to save the metric values to a file, then write a small example which reads them and combines the metrics?

As for providing the source code, you can copy it into a post. That can affect formatting and indentation, so the best way is to put it in a public repository and provide the link to it.

---

<div class="post-metadata">

### Author: ![tvanwalsum](https://discourse.itk.org/letter_avatar_proxy/v4/letter/t/a87d85/32.png) [@tvanwalsum](https://discourse.itk.org/u/tvanwalsum)
#### Post date: [December 12, 2018, 10:12am UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/9 "2018-12-12T10:12:01Z")

</div>

Yes, I understand.

However, in this case I’m using elastix (elastix.isi.uu.nl), which is a registration software that uses itk. I’m using that program, and not programming it myself. I think I could provide a stripped-down registration example (fake images and registration parameter file) but I certainly will not be able to adapt the code as you suggested.

Main reason for posting was hoping that someone in the itk-community (possibly also with elastix experience) recognizes this problem, or knows what relevant changes have happened in itk when going from 4.8.2 to 4.9.1 (something with multi-threading?) that may have caused this problem.

Best,

Theo

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 12, 2018, 3:30pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/10 "2018-12-12T15:30:08Z")

</div>

@Niels_Dekker @kaspermarstal @stefanklein @mstaring @denis.p.shamonin Can you pitch in?

---

<div class="post-metadata">

### Author: ![kaspermarstal](https://discourse.itk.org/user_avatar/discourse.itk.org/kaspermarstal/32/211_2.png) [@kaspermarstal](https://discourse.itk.org/u/kaspermarstal)
#### Post date: [December 17, 2018, 11:21am UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/11 "2018-12-17T11:21:40Z")

</div>

Theo was so kind as to make us aware of the issue and we suggested that he posted here in case someone experienced similar issues and had an idea where the regression might come from. It is on our todo list and just wanted to gather as much information as possible.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 17, 2018, 3:40pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/12 "2018-12-17T15:40:09Z")

</div>

I am unaware of anything that could have caused this. It we were we would likely have considered alternatives.

I would suggest narrowing down to a small Elasitx invocation that causes this 3-4x slow down. Then use [git-bisect](https://git-scm.com/docs/git-bisect) to narrow down the offending ITK commit range. A script will have to be created to compile Elasitx against the ITK version git is bisected, then run your Elastix invocation to test the timing.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 22, 2018, 11:55am UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/13 "2018-12-22T11:55:56Z")

</div>

FYI, I’m still busy trying to find what caused the performance regression reported by @tvanwalsum But at least, I can reproduce the issue. It must have been introduced somewhere between [4.8.2](https://github.com/Kitware/ITK/tree/v4.8.2) and [4.9 release candidate 2](https://github.com/Kitware/ITK/tree/v4.9rc02):

That is, between:

[74155d0b7059fde841832c92a9aec78c79bc68c0](https://github.com/Kitware/ITK/commit/74155d0b7059fde841832c92a9aec78c79bc68c0)  
Matt McCormick, 2015-11-12  
ENH: Bump CMakeLists.txt version to 4.8.2.

And:

[b5ac097618781214edada6649563d2b18a19f99f](https://github.com/Kitware/ITK/commit/b5ac097618781214edada6649563d2b18a19f99f)  
Matt McCormick, 2015-12-01  
Merge topic ‘CppTest’

To be continued!

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 23, 2018, 3:14pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/14 "2018-12-23T15:14:45Z")

</div>

Update: after having tested the example case from Theo (@tvanwalsum), I think I may now have found the cause of the performance issue 🤩 Theo reported here that [elastix](https://github.com/SuperElastix/elastix) performed _3 to 4 times slower_ with ITK \>= 4.9 than with ITK 4.8. 😮

So this is what I found: Elastix internally makes extensive use of [itk::TimeProbe](https://itk.org/Doxygen/html/classitk_1_1TimeProbe.html): it often creates a local `itk::TimeProbe` object. This class is derived from [itk::ResourceProbe](https://itk.org/Doxygen/html/classitk_1_1ResourceProbe.html). With ITK 4.9, the `ResourceProbe()` constructor has an extra function call, that was not yet in ITK 4.8: `GetSystemInformation()`. This function call appears _very time consuming_ (at least that’s what I observed on VS2013 + Windows 10).

ITK 4.9.0 itk::ResourceProbe() constructor calls GetSystemInformation() and Reset():  
[https://github.com/InsightSoftwareConsortium/ITK/blob/v4.9.0/Modules/Core/Common/include/itkResourceProbe.hxx#L51](https://github.com/InsightSoftwareConsortium/ITK/blob/v4.9.0/Modules/Core/Common/include/itkResourceProbe.hxx#L51)

ITK 4.8.2 itk::ResourceProbe() constructor only just calls Reset():  
[https://github.com/InsightSoftwareConsortium/ITK/blob/v4.8.2/Modules/Core/Common/include/itkResourceProbe.hxx#L42](https://github.com/InsightSoftwareConsortium/ITK/blob/v4.8.2/Modules/Core/Common/include/itkResourceProbe.hxx#L42)

So now I’m wondering how to fix this issue. Is it really necessary for the constructor of `itk::TimeProbe` to always call `GetSystemInformation()`?

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 23, 2018, 8:37pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/15 "2018-12-23T20:37:03Z")

</div>

Just submitted 🙂

> <https://github.com/InsightSoftwareConsortium/ITK/issues/350>

I just hope that a fix can still be included with ITK 5!

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [December 23, 2018, 9:07pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/16 "2018-12-23T21:07:35Z")

</div>

Awesome job tracking that down!!

Delaying that call makes since. Does the information change? Can some of it be called once and saved into a static member?

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 23, 2018, 10:08pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/17 "2018-12-23T22:08:55Z")

</div>

Thanks, Brad. Honestly I don’t know much about these Probe classes. Elastix just does:

```
itk::TimeProbe timer;
timer.Start();
// Do some time-consuming task here...
timer.Stop();
// ...and then print timer.GetMean() as "ms".

```

For such a use case, I don’t think `GetSystemInformation()` is necessary at all.

Clearly, the system info is necessary for [itk::ResourceProbe::PrintSystemInformation()](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b03/Modules/Core/Common/include/itkResourceProbe.hxx#L261). If this is the only place where the system info is used, I think it would be better to get it there, instead of in the default-constructor.

It’s probably possible, technically, to store the system info in static data members. But I have to say, I’m not a fan of static data. If this `PrintSystemInformation()` is in practice rarely called, I think it’s not nice to keep its data “alive” for the entire run of the process.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [December 24, 2018, 2:53pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/18 "2018-12-24T14:53:55Z")

</div>

> [@Niels\_Dekker](#):
>
> the system info is necessary for [itk::ResourceProbe::PrintSystemInformation()](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b03/Modules/Core/Common/include/itkResourceProbe.hxx#L261). If this is the only place where the system info is used

… then the system information could be stored in a local static variable, which will be instantiated only if the `Print` method is called.

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 24, 2018, 3:17pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/19 "2018-12-24T15:17:45Z")

</div>

@dzenanz In the mean time I saw that PrintSystemInformation() is not the only ResourceProbe member function using system info. The other one is [itk::ResourceProbe::PrintJSONSystemInformation(os)](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.0b03/Modules/Core/Common/include/itkResourceProbe.hxx#L574).

I’m preparing a fix now, please wait 🙂

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [December 24, 2018, 4:06pm UTC](https://discourse.itk.org/t/elasitx-performance-issue-with-itk-4-8/1467/20 "2018-12-24T16:06:49Z")

</div>

Please review:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/351>

I hope it can be included with ITK 5.0 (and possibly also ITK 4.13.2…?)
