# Looking for ways to optimize code for faster execution

**URL:** <https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431>\
**Category:** Garfield++\
**Created:** [September 14, 2020, 9:18am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431 "2020-09-14T09:18:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pete](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@Pete](https://root-forum.cern.ch/u/Pete)\
**Post date:** [September 14, 2020, 9:18am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/1 "2020-09-14T09:18:31Z")

</div>

I am simulating an electron avalanche using the same method as shown in the GEM example. For my case, there are two main differences:

1. Instead of creating 10 random electrons at a given z0, the primary electrons are created from a track using Heed.
2. Instead of using the AvalancheMC to drift the ions, I use DriftLineRKF. This seems to speed things up by a little and doesn’t seem to create any significant difference to the signals.

As expected, this increases the runtime by a significant amount comparing to the example shown in GEM. Here is the part of my code that is responsible for creating the avalanche and drifting ions:

```auto
 double xc = 0., yc = 0., zc = 0., tc = 0., ec = 0., extra = 0.;
 int nc = 0;
 while (track.GetCluster(xc, yc, zc, tc, nc, ec, extra)) {`
    for (int k = 0; k < nc; ++k) {
      double xe = 0., ye = 0., ze = 0., te = 0., ee = 0.;
      double dx = 0., dy = 0., dz = 0.;
      track.GetElectron(k, xe, ye, ze, te, ee, dx, dy, dz);
      aval.AvalancheElectron(xe, ye, ze, te, ee, dx, dy, dz);
      const int np = aval.GetNumberOfElectronEndpoints();
      double xe1, ye1, ze1, te1, e1;
      double xe2, ye2, ze2, te2, e2;
      double xi1, yi1, zi1, ti1;
      double xi2, yi2, zi2, ti2;
      int status;
      for (int j = np; j--;) {
        aval.GetElectronEndpoint(j, xe1, ye1, ze1, te1, e1, 
                                    xe2, ye2, ze2, te2, e2, status);
        drift.DriftIon(xe1, ye1, ze1, te1);
      }
    }
  }

```

Is there any way to make this part of the program run faster?

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [September 15, 2020, 6:54am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/2 "2020-09-15T06:54:55Z")

</div>

@hschindl Perhaps you have an idea?

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [September 15, 2020, 11:33am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/3 "2020-09-15T11:33:45Z")

</div>

Hi,  
apologies for the delayed response. It’s a very good question but I’m afraid I probably don’t have a good answer.

If your program is derived from the GEM example, then one trivial thing you can do (and which you’ve probably done already) is to switch off visualization of the drift lines.  
A significant fraction of the time is typically spent on the look-up/interpolation of the electric field. It therefore helps if - by smart meshing or exploiting symmetries of your geometry - you somehow manage to reduce the size of your field map, but that’s of course not always easy.  
You could also set an upper limit of the number of electrons in the avalanche using the function `EnableAvalancheSizeLimit`, but that does affect your signal.  
There is definitely also significant room for speeding up the code by making better use of modern CPUs and using modern programming techniques (but this will need some work).  
Best regards,  
Heinrich

---

<div class="post-metadata">

**Author:** ![Pete](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@Pete](https://root-forum.cern.ch/u/Pete)\
**Post date:** [September 15, 2020, 9:37pm UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/4 "2020-09-15T21:37:58Z")

</div>

Hi,

Thank you for your reply. I am not sure if this is related, but I have a follow-up question.

When my code is running. It often outputs this:

```auto
MediumMagboltz::GetElectronCollisionRate:
    Rate at 43.9911 eV is not included in the current table.
    Increasing energy range to 46.1907 eV.
AvalancheMicroscopic::TransportElectron: Increasing null-collision rate by 5%.

```

What does this mean? Does it slow down my program if it happens too often?

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [September 16, 2020, 7:59am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/5 "2020-09-16T07:59:37Z")

</div>

Hi,  
by default, MediumMagboltz creates a table of the electron-atom/molecule cross-sections from 0 to 40 eV. If an electron energy higher than that is encountered, this table is updated, with the upper limit of the table increased by 5%. This will indeed slow down a bit the first avalanche. To avoid this from happening, you could set the upper energy limit upfront, e. g. to 200 eV:

```auto
gas.SetMaxElectronEnergy(200.);

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 30, 2020, 7:59am UTC](https://root-forum.cern.ch/t/looking-for-ways-to-optimize-code-for-faster-execution/41431/6 "2020-09-30T07:59:41Z")

</div>

Closed on September 30, 2020, 7:59am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
