classification
Title: tkinter with Tk 8.6.11 is slow on macOS
Type: performance Stage:
Components: macOS, Tkinter Versions: Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: chrstphrchvz, epaine, ned.deily, ronaldoussoren, serhiy.storchaka, thomaswamm
Priority: normal Keywords:

Created on 2021-03-16 06:13 by thomaswamm, last changed 2021-04-05 04:59 by thomaswamm.

Files
File name Uploaded Description Edit
tkbench.zip epaine, 2021-03-23 14:55
results-tkbench.txt thomaswamm, 2021-03-24 12:06 Terminal transcript of tk benchmarks on M1 Mac Mini
results_tk-bench_Win10_i5-8250U.txt thomaswamm, 2021-03-24 21:44 Terminal transcript of bench.py on 3 Win10 pythons
Messages (16)
msg388822 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-16 06:13
Comparing performance of a Tkinter graphics program with Python 3.9.2 on an M1 Mac Mini, I find it to be slower than earlier versions, and much slower than the same program running on other computers such as a Raspberry Pi 3B (Python 3.7.3).

Initial investigation suggests the slow down is because of Tk 8.6.11.
The same program on Windows 10 on Intel i5-8250U is at least 10x faster, contrary to expectations.

Has anyone noticed similar?

The program is simple & portable, downloadable from: 
https://github.com/ThomasWamm/TerraLunar.git
All it does is 2-D orbital mechanics simulation, to teach me Python.
msg388829 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-16 10:23
An easy demo of the performance problem is to compare the execution speed of the single command: 

>>> help('modules')

in a python3 shell in a terminal window (it's real fast), 
vs. inside the IDLE3 shell (it's terribly slow).

I understand that IDLE3 uses the tkinter.py/Tcl/Tk toolset for windowing. There's something slowing it down on the M1 Mac Mini (MacOS 11.2.3), making it run much slower than on a Wintel PC or Linux Raspberry Pi.

I don't know if the problem occurs with any Python versions other than 3.9.2 on MacOS.
msg388905 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2021-03-17 04:12
Thanks for submitting a very interesting issue! tkinter/Tk performance is not something we normally pay a lot of attention to. I did not attempt to run your application but I was able to see significant performance differences with your simple "help('modules') test. Before saying more, I want to test some additional configurations but I've run out of time for today. However, the differences don't seem to have anything to do with running on new M1 Macs as I was able to see them on a traditional Intel-based Mac running macOS 11.
msg388917 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-17 11:12
I spent a few hours running numerous configurations (M1 Mac Mini, iMac24-2007, Win10 on i5-8250U, Raspberry Pi 4B, various Pythons from 3.7.3 to 3.10.0a6). It can get confusing. 
The primary interesting result is that Python 3.9.2 and 3.10.0a6 arm64 versions on M1 Mac Mini are the absolute worst performers by factors of 2 to 15, when relying on tkinter/Tcl/Tk for windows or graphics. Any Python3 on Windows 10 on an Intel i5 was at least 8x faster than the M1 Mac.  Intel Python 3.9.2 beats arm64 Python 3.9.2 or 3.10.0a6.  The problem is somewhere in the tkinter/Tcl/Tk layers or their connection to MacOS and arm64.  Python 3.7.3 on Raspberry OS (Linux) performs in the middle between slow MacOS and fast Windows.
The fastest configuration I found on the M1 Mac was Python 3.8.2 (Intel code) in combination with deprecated Tcl/Tk 8.5.9; that config was about 8x faster than 3.9.2 arm64 with Tcl/Tk 8.6.11.

The simplest qualitative performance test is just use 
>>> help('modules')   (in IDLE vs. python in a Terminal window) 
though my TerraLunar.py graphics program can give quantitative results.

The Turtle graphics demo programs in IDLE also perform slow on the M1 Mac.
msg388957 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-03-17 18:48
The result of help('modules') depends on Python version. Do you have any example which does not depend on Python version?

Could you also run some pure Tcl/Tk demos to check whether the difference is in Tcl/Tk instead of the wrapper?
msg389383 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-23 10:53
Summary: "poor tkinter performance correlates with failed self-tests"

I'm an amateur hobbyist noobie programmer, so sophisticated testing is a challenge for me.  I raised this issue because I noticed Python graphics thru tkinter is especially slow on MacOS, and especially ultra-slow on my new M1 Mac Mini with Python 3.9.2 or 3.10.0a6 for Apple silicon. Aside from this weird issue, the M1 Mac outperforms all my other computers (including Win10 on i5-9500T, and Raspberry Pi 400). I'm expecting at least 10x performance improvement on M1 Macs when this issue gets resolved. 

Anyway, I looked under the hood.  I found some tkinter test programs runnable on MacOS with: 

/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/tkinter/test/runtktests.py

The same program is buried in the Windows Python modules library, but was not findable in Python 3.7.3 for Linux on Raspberry Pi.

So I ran runtktests.py on Windows 10 and MacOS in many configurations. All was good EXCEPT Python 3.9.2 and 3.10.0a6 on old MacOS 10.11.6 and new MacOS 11.2.3.  Those configs report about 10 Failed tests in a suite of over 1000 tests, and interestingly the 3.9 and 3.10 tests run about 4x slower than the older Python 3.8 tests that reported zero failures.

So poor tkinter performance on MacOS correlates with failed tkinter self-tests. Maybe a more expert person could dig deeper?
msg389384 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-23 11:11
Failed tkinter tests were already noted in Issue 42507.
No mention of failures correlating with much slower testing.
msg389389 - (view) Author: E. Paine (epaine) * Date: 2021-03-23 14:55
This *appears* to be a Tk issue. I ran a benchmark (see attached) which repeatedly performed canvas actions. The first test ("clearing") clears the canvas, creates a rectangle, then calls both `update` and `update_idletasks`. The second test ("not clearing") is the same as the above but did not clear the canvas (causing rectangles to be drawn over each other). Here is a table of the results (times are per iteration in milliseconds):

+------------+-------------------------+-------------------------+
| Tk Version | Python                  | Wish                    |
|            +----------+--------------+----------+--------------+
|            | clearing | not clearing | clearing | not clearing |
+============+==========+==============+==========+==============+
| 8.6.8      | 6.6      | 41.4         | 4.2      | 16.2         |
+------------+----------+--------------+----------+--------------+
| 8.6.11     | 37.0     | 37.1         | 36.1     | 36.1         |
+------------+----------+--------------+----------+--------------+

A few notes:
- These were performed on a slow machine and results could be influenced by background CPU usage fluctuations
- Because it is a slow computer, I limited the iterations to just 1000 per test.
- Python version was 3.9.2
- Wish 8.6.8 was installed using an ActiveTcl binary whereas 8.6.11 was installed through homebrew
msg389446 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-24 12:06
I ran the bench.py and bench.tcl programs from E. Paine (epaine) on my M1 Mac Mini.  Attached is a commented Terminal transcript.

The tl;dr summary again is that Python 3.10.0a6 arm64 for Apple silicon is 3 to 100 times SLOWER than earlier pythons compiled for Intel x86-64 running with Rosetta2, when doing window operations thru Tcl/Tk.

I have not yet attempted to install any version of Tcl/Tk independent of any Python or Apple build.  I'm still learning slowly.  And I am inspired to re-jig my little TerraLunar graphics program to run with python2.7 arm64.
msg389447 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-03-24 12:17
If this is a Tk issue we should report it in their bug tracker.

I'm definitely against switching to an older version of Tk, the most recent release fixes a lot of problems with Tk on macOS.  Correctness before performance.

How bad is the performance compared to Tk on other platforms (with similar hardware)?
msg389448 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2021-03-24 12:37
One thing to look into: there appears to be a tk 8.6.11.1 release, I have no idea what changed in this micro release.
msg389450 - (view) Author: E. Paine (epaine) * Date: 2021-03-24 13:15
The reduction in speed with later Tk versions does not seem to occur on Linux. See the below table (times are microseconds per iteration - tested using wish):

+------------+----------+--------------+
| Tk Version | clearing | not clearing |
+============+==========+==============+
| 8.6.8      | 197.7    | 183.46       |
+------------+----------+--------------+
| 8.6.11     | 170.3    | 201.6        |
+------------+----------+--------------+

I have not tested on Windows but I don't think it's unreasonable to conclude the performance issue is limited to MacOS.
msg389477 - (view) Author: E. Paine (epaine) * Date: 2021-03-24 17:03
> there appears to be a tk 8.6.11.1 release

This was the version I used when testing Wish on both MacOS and Linux.
msg389478 - (view) Author: E. Paine (epaine) * Date: 2021-03-24 17:29
As this appears to be a Tk issue, I have created https://core.tcl-lang.org/tk/tktview/f642d7c0f4.
msg389490 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-03-24 21:44
tkinter performance on Windows 10 looks okay, no obvious problems.

Attached is a transcript showing bench.py results for Pythons 3.8.2, 3.9.2, and 3.10.0a6 on Windows 10 on a Dell laptop with an Intel i5-8250U CPU (medium good speed, a good comparison to Apple M1 Mac).

I originally raised this Issue because my new M1 Mac Mini performed so poorly compared to my Windows laptop, only for tkinter-related code.
msg390224 - (view) Author: Thomas Wamm (thomaswamm) Date: 2021-04-05 04:59
I have forked my TerraLunar graphics program to make a simpler more portable version, called "TerraLunar-tkbench.py" to facilitate testing of the tkinter stack for graphics performance.  The project folder is at: 

https://github.com/ThomasWamm/TerraLunar-tkbench.git

The folder includes results and commentary from running the program on numerous platforms (MacOS, Windows10, Raspberry OS Linux) and Python versions.
Here are the most interesting summary results:
------------------------------------------------------------------------
Apple M1 Mac Mini MacOS 11.2.3 (arm64 Apple Silicon, or Rosetta2 x86_64)

  Python 3.8.2    Tcl/Tk 8.5.9  TerraLunar-tkbench 0.1.8  runtime = 37   (arm64)

  Python 3.8.7    Tcl/Tk 8.6.8  TerraLunar-tkbench 0.1.8  runtime = 69   (x86_64)

  Python 3.9.2    Tcl/Tk 8.6.8  TerraLunar-tkbench 0.1.8  runtime = 69   (x86_64)

* Python 3.10.0a6 Tcl/Tk 8.6.11 TerraLunar-tkbench 0.1.8  runtime = 421  (arm64) *
*   Why is Tcl/Tk 8.6.11 on arm64 so slow?


old iMac24-2007  MacOS 10.11.6  on 2.4 GHz Intel Core 2 Duo (x86_64)

  Python 3.7.3     Tcl/Tk 8.6.8   TerraLunar-tkbench 0.1.8  runtime = 381
  Python 3.8.2     Tcl/Tk 8.6.8   TerraLunar-tkbench 0.1.8  runtime = 377
  Python 3.9.2     Tcl/Tk 8.6.8   TerraLunar-tkbench 0.1.8  runtime = 379

* Python 3.10.0a6  Tcl/Tk 8.6.11  TerraLunar-tkbench 0.1.8  runtime = 292 *
* this is faster than the M1 Mac, differing in silicon and MacOS version.


Dell laptop Windows10 i5-8250U  >= 1.6 GHz quad-core x86_64

  Python 3.9.2    Tcl/Tk 8.6.9   TerraLunar-tkbench 0.1.8  runtime = 7
  Python 3.10.0a6 Tcl/Tk 8.6.10  TerraLunar-tkbench 0.1.8  runtime = 7

# Windows10 seems to be the best environment for tkinter.py / Tcl / Tk graphics apps.
----------------------------------------------------------------------

Every Mac platform is sadly much slower than a comparable Windows platform, for tkinter graphics. But the new M1 Mac platform is the slowest by far, slower than even a much older Intel Mac running the same versions of Python 3.10.0a6 and Tk 8.6.11

That's enough involvement for this noobie; I've now learned how complicated this performance issue is. It's not on my critical path, so I'm going back to my regular project far removed from tkinter graphics.  Good Luck folks!
...thomas
History
Date User Action Args
2021-04-05 04:59:27thomaswammsetmessages: + msg390224
2021-03-25 20:27:36chrstphrchvzsetnosy: + chrstphrchvz
2021-03-24 21:44:06thomaswammsetfiles: + results_tk-bench_Win10_i5-8250U.txt

messages: + msg389490
2021-03-24 17:29:21epainesetmessages: + msg389478
2021-03-24 17:03:28epainesetmessages: + msg389477
2021-03-24 13:15:11epainesetmessages: + msg389450
2021-03-24 12:37:11ronaldoussorensetmessages: + msg389448
2021-03-24 12:17:49ronaldoussorensetmessages: + msg389447
2021-03-24 12:06:14thomaswammsetfiles: + results-tkbench.txt

messages: + msg389446
2021-03-23 14:55:17epainesetfiles: + tkbench.zip

messages: + msg389389
2021-03-23 11:11:58thomaswammsetmessages: + msg389384
2021-03-23 10:53:46thomaswammsetmessages: + msg389383
2021-03-17 18:48:42serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg388957
2021-03-17 11:12:14thomaswammsetmessages: + msg388917
2021-03-17 04:12:05ned.deilysetmessages: + msg388905
title: Tk 8.6.11 slow on M1 Mac Mini MacOS Python 3.9.2 native ARM version -> tkinter with Tk 8.6.11 is slow on macOS
2021-03-16 10:27:25epainesetnosy: + ronaldoussoren, ned.deily, epaine

components: + macOS
versions: + Python 3.10
2021-03-16 10:23:05thomaswammsetmessages: + msg388829
2021-03-16 06:13:14thomaswammcreate