Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkinter with Tk 8.6.11 is slow on macOS #87677

Closed
thomaswamm mannequin opened this issue Mar 16, 2021 · 20 comments
Closed

tkinter with Tk 8.6.11 is slow on macOS #87677

thomaswamm mannequin opened this issue Mar 16, 2021 · 20 comments
Labels
3.9 only security fixes 3.10 only security fixes OS-mac performance Performance or resource usage topic-tkinter

Comments

@thomaswamm
Copy link
Mannequin

thomaswamm mannequin commented Mar 16, 2021

BPO 43511
Nosy @ronaldoussoren, @ned-deily, @aivarannamaa, @serhiy-storchaka, @chrstphrchvz, @E-Paine
Files
  • tkbench.zip
  • results-tkbench.txt: Terminal transcript of tk benchmarks on M1 Mac Mini
  • results_tk-bench_Win10_i5-8250U.txt: Terminal transcript of bench.py on 3 Win10 pythons
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-03-16.06:13:14.383>
    labels = ['OS-mac', 'expert-tkinter', '3.9', '3.10', 'performance']
    title = 'tkinter with Tk 8.6.11 is slow on macOS'
    updated_at = <Date 2021-04-22.12:16:27.487>
    user = 'https://bugs.python.org/thomaswamm'

    bugs.python.org fields:

    activity = <Date 2021-04-22.12:16:27.487>
    actor = 'Aivar.Annamaa'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['macOS', 'Tkinter']
    creation = <Date 2021-03-16.06:13:14.383>
    creator = 'thomaswamm'
    dependencies = []
    files = ['49905', '49908', '49913']
    hgrepos = []
    issue_num = 43511
    keywords = []
    message_count = 17.0
    messages = ['388822', '388829', '388905', '388917', '388957', '389383', '389384', '389389', '389446', '389447', '389448', '389450', '389477', '389478', '389490', '390224', '391593']
    nosy_count = 7.0
    nosy_names = ['ronaldoussoren', 'ned.deily', 'Aivar.Annamaa', 'serhiy.storchaka', 'chrstphrchvz', 'epaine', 'thomaswamm']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue43511'
    versions = ['Python 3.9', 'Python 3.10']

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 16, 2021

    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.

    @thomaswamm thomaswamm mannequin added 3.9 only security fixes topic-tkinter performance Performance or resource usage labels Mar 16, 2021
    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 16, 2021

    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.

    @E-Paine E-Paine mannequin added OS-mac 3.10 only security fixes labels Mar 16, 2021
    @ned-deily
    Copy link
    Member

    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.

    @ned-deily ned-deily changed the 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 Mar 17, 2021
    @ned-deily ned-deily changed the 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 Mar 17, 2021
    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 17, 2021

    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.

    @serhiy-storchaka
    Copy link
    Member

    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?

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 23, 2021

    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?

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 23, 2021

    Failed tkinter tests were already noted in bpo-42507.
    No mention of failures correlating with much slower testing.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Mar 23, 2021

    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

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 24, 2021

    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.

    @ronaldoussoren
    Copy link
    Contributor

    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)?

    @ronaldoussoren
    Copy link
    Contributor

    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.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Mar 24, 2021

    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.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Mar 24, 2021

    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.

    @E-Paine
    Copy link
    Mannequin

    E-Paine mannequin commented Mar 24, 2021

    As this appears to be a Tk issue, I have created https://core.tcl-lang.org/tk/tktview/f642d7c0f4.

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Mar 24, 2021

    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.

    @thomaswamm
    Copy link
    Mannequin Author

    thomaswamm mannequin commented Apr 5, 2021

    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

    @aivarannamaa
    Copy link
    Mannequin

    aivarannamaa mannequin commented Apr 22, 2021

    According to Tk people, the regression in performance was unavoidable and the solution is to avoid relying on update in a loop: https://core.tcl-lang.org/tk/tktview/f642d7c0f4

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @ronaldoussoren
    Copy link
    Contributor

    @ned-deily : I propose closing this issue because this an issue with Tk and not something we can fix in CPython.

    @serhiy-storchaka
    Copy link
    Member

    Looking at the difference between macOS and Linux (4 vs 200!), it seems to me, that Tk "cheated" on macOS. Now it "cheats" less.

    @ned-deily
    Copy link
    Member

    I propose closing this issue because this an issue with Tk and not something we can fix in CPython.

    I agree.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes OS-mac performance Performance or resource usage topic-tkinter
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants