# Python with ROOT.Math libraries is very slow on MAc

**URL:** <https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689>\
**Category:** Newbie\
**Created:** [September 6, 2021, 2:19am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689 "2021-09-06T02:19:49Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 2:19am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/1 "2021-09-06T02:19:49Z")

</div>

_ROOT Version:_ 6.24/04  
_Platform:_ OSX 20.26.7, Catalina  
_Compiler:_ precompiled installation

I noticed that my python script with ROOT libraries is running much slower on Mac (10.16.7, Catalina) then on RedHat7.  
I found that it is connected with using ROOT.Math library. I wrote simple script to test it.

```auto
#!/usr/local/bin/python3
#import sys, ROOT, math
#import ctypes
import math
import ROOT
from ROOT import gBenchmark
n=0
gBenchmark.Start('lund')
for i in range(50000):
    n=n+1
# 1 sqrt3=math.sqrt(2.)
# 2 sqrt2=ROOT.Math.sqrt(2.)
    if n%10000 ==0:
          print('n # ',n)
gBenchmark.Show('lund')

```

There are 2 operators in loop that are calculating just sqrt(2.).  
I made 3 timing measurements using gBenchmark.

1. Without both of them – 0.01 seconds
2. sqrt3=math.sqrt(2.) -Only this operator, very fast, ~0.01 seconds
3. sqrt2=ROOT.Math.sqrt(2.) – Only this operator, very slow ~12 secons, 1200 times!!! slower than #2.  
The result doesn’t change much if I am using homebrew, port or direct from cern.root website.  
I have old Mac laptop with ROOT 6.24/00, python 2.7.16. The same script is working very fast.  
I contacted with some IT experts. They don’t ay me much exsperts that there is definitely some problem.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 6, 2021, 6:30am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/2 "2021-09-06T06:30:07Z")

</div>

May be @etejedor can help

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 6, 2021, 7:10am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/3 "2021-09-06T07:10:27Z")

</div>

Hello,

Is this reproducible also in C++?

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 6, 2021, 9:23am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/4 "2021-09-06T09:23:20Z")

</div>

Random guess, but as I expect `math.sqrt` and `ROOT.Math.sqrt` to both be optimized to death, I think what you might be measuring is the overhead of calling C++ functions via PyROOT compared to calling standard Python C extension modules (I don’t know why that might be slower on Mac than on RedHat).

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 6, 2021, 10:07am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/5 "2021-09-06T10:07:57Z")

</div>

You can try importing sqrt from `ROOT.Math` and calling it directly from the loop:

```python
import ROOT
from ROOT.Math import sqrt as root_sqrt

for i ...
   r = root_sqrt(2.)

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 6, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/6 "2021-09-06T13:27:55Z")

</div>

Split this topic on September 6, 2021, 1:27pm UTC

4 posts were split to a new topic: [`import ROOT` is slow](https://root-forum.cern.ch/t/import-root-is-slow/46699)

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 1:25pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/10 "2021-09-06T13:25:26Z")

</div>

I did not test it myself but my friend did and said that c++ works fast.

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 1:30pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/11 "2021-09-06T13:30:21Z")

</div>

It is not the case. I have another laptop with python2.7 and ROOT v6.14.  
It works very fast:  
math.sqrt(2.). 0.02 sec  
ROOT.Math.sqrt(2.) 0.2 sec

As I wrote with with python3.9.6 and ROOT v6.24  
math.sqrt(2.). 0.02 sec  
ROOT.Math.sqrt(2.) 12.8 sec

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 1:35pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/12 "2021-09-06T13:35:11Z")

</div>

> [@etejedor](#):
>
> `from ROOT.Math import sqrt as root_sqrt`

I did it but got the error message  
sqrt3=root.sqrt(2.)  
NameError: name ‘root’ is not defined

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 1:38pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/13 "2021-09-06T13:38:49Z")

</div>

> [@diemort](#):
>
> from ROOT import TFile

In my case adding the line  
from ROOT import TFile  
does nothing. CPU time is the same

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 6, 2021, 2:03pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/14 "2021-09-06T14:03:11Z")

</div>

> [@valkuba](#):
>
> As I wrote with with python3.9.6 and ROOT v6.24  
> math.sqrt(2.). 0.02 sec  
> ROOT.Math.sqrt(2.) 12.8 sec

Yes I understand, my point was that it’s probably not ROOT’s implementation of `sqrt` that’s slow(er), but the machinery required to call the C++ `sqrt` function from Python, sorry if that’s just obvious.

> [@valkuba](#):
>
> I did it but got the error message  
> sqrt3=root.sqrt(2.)  
> NameError: name ‘root’ is not defined

should be `root_sqrt`, same as the name you imported the function as

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 6, 2021, 5:22pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/15 "2021-09-06T17:22:32Z")

</div>

Sorry for typo.  
Yes, this helps, 0.06 seconds instead of 13 seconds.  
This is interesting suggestion and observation.  
It was just a example. In the real program I am using another packages.  
ROOT.Math.PxPyPzMVector  
Do I need to import all of them one by one?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [September 7, 2021, 10:04am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/16 "2021-09-07T10:04:05Z")

</div>

So it seems that it was the lookups which consumed the time (`ROOT.Math` and `ROOT.Math.sqrt`). It might be then good to just import what you need before using it in a hot loop.

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [September 7, 2021, 6:59pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/17 "2021-09-07T18:59:24Z")

</div>

@valkuba Yes, in old ROOT.py, lookup results on the facade were cached, whereas in “new” PyROOT, successful top-level lookups no longer are, hence the difference you observe between those two ROOT versions. I.e. it’s a choice (bug) in the new approach, not a Mac-thingy (or even Python-thingy).

To get the old speed back, you only ever need to do ‘from ROOT import Math’ however, since ‘Math’ is bound by `cppyy`, which does cache successful lookups, so `Math.sqrt` will be fast (even as, yes, just `sqrt` will be even faster, but the same is true for use of `sqrt` from `math.sqrt`).

(Aside, on my Linux box, using `**0.5` outperforms all and `cppyy.gbl.std.sqrt` has an additional slowdown of 25% b/c it’s a templated function, which results in an extra internal lookup.)

The remaining performance differences between `Math.sqrt` and `math.sqrt` you’re left with are:

1. wrapper generation for cppyy on first call
2. `Math.sqrt` being an overloaded function whereas `math.sqrt` only works on `double`
3. For python3: `math.sqrt` benefiting from an optimized call API, which isn’t available (in alternate form) to `cppyy` until python3.8 and only actually used as of `cppyy` 2.1.0.

And yes, `sqrt` is my favorite function when trying to understand call overhead 🙂

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 7, 2021, 7:42pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/18 "2021-09-07T19:42:51Z")

</div>

> [@wlav](#):
>
> in old ROOT.py, lookup results on the facade were cached, whereas in “new” PyROOT, successful top-level lookups no longer are, hence the difference you observe between those two ROOT versions. I.e. it’s a choice (bug) in the new approach

Should we fix this in ROOT.py?

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [September 7, 2021, 8:06pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/19 "2021-09-07T20:06:28Z")

</div>

> [@eguiraud](#):
>
> Should we fix this in ROOT.py?

You mean to make old PyROOT equally slow by removing the caching? Personally, I would leave old `ROOT.py` well alone and instead opt to add caching to the new `ROOT/_facade.py`, as a way to equalize old and new behavior. Two orders of magnitude for a common use case is nothing to sneeze at and fixing `ROOT/_facade.py` is certainly easier than asking folks all over to change a decade and a half worth of legacy codes. Besides, recommendations in preference of `from X import Y` over `import X; X.Y` have varried over time. Personally, I used to like the former but now prefer the latter b/c of Jupyter notebooks.

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 7, 2021, 9:34pm UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/20 "2021-09-07T21:34:36Z")

</div>

Thank you very much for your suggestion.  
It really works. But I am still wondering why I don’t need to make such a trick  
with my old laptop where I have python2.7 and ROOT v6.14?

Thanks again for your help,  
Valery

---

<div class="post-metadata">

**Author:** ![valkuba](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/v/e99b99/32.png) [@valkuba](https://root-forum.cern.ch/u/valkuba)\
**Post date:** [September 8, 2021, 2:12am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/21 "2021-09-08T02:12:28Z")

</div>

It is very impressive explanation. I cannot say that I understood details due to education in physics, not computer science.

> [@wlav](#):
>
> from ROOT import Math

unfortunately does not work for me actually.What really helps is

from ROOT.Math import sqrt as root\_sqrt

and then you have to edit all calls to ROOT.Math functions in your script.  
As you understand sqrt is just an example. In reality the program contains many other calls to ROOT.Math.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 8, 2021, 7:05am UTC](https://root-forum.cern.ch/t/python-with-root-math-libraries-is-very-slow-on-mac/46689/22 "2021-09-08T07:05:05Z")

</div>

`s:ROOT.py:ROOT/_facade.py` in my comment above, of course… 😃

Thanks for the explanation!
