Let's install Python on the new Mac M1 Pro and benchmark it

Let's install Python on the new Mac M1 Pro and benchmark it

Installing Python on M1 Mac and checking the execution speed)

Installing Python on M1 Macs

Many of the Macs already on the market have moved from Intel processors to Apple silicon (the M1 chip). The performance has improved significantly, and from now on, Macs with Apple silicon will become mainstream.

There are two ways to install Python on M1 Mac, as before, using the installer provided by Python official, and installing by entering commands from the terminal using Homebrew etc. There is a system. First, let's check how to install Python on macOS.

Installing Python using the installer

If you want to install Python using the installer, download it from the Python official site. At the time of writing the manuscript, 3.10.0 is the latest, the installer is a universal binary, and the package is compatible with both Intel Mac and M1 Mac.

Double-click the downloaded pkg file to start the installer. After that, basically click the [Continue] button a few times to complete the installation. The easiest and surest way is to use the installer.

In addition, I was wondering if it was really an M1 compatible version, so I ran Python with a simple editor or an interactive execution environment IDLE and checked it with the activity monitor. I tried it with Python3.10, but "Apple" is displayed in the type, and I can see that it corresponds to M1. It is easy to understand because "Intel" is displayed for applications that run on the Intel compatibility layer.

Installing with Homebrew

Next, I'll show you how to install with Homebrew. Many programmers often use Homebrew to install Python. With Homebrew, you can install the necessary software and libraries with a single command, so it's convenient to install not only Python but also multiple tools.

Python to the new Mac M1 Pro Install and benchmark

If you are installing with Homebrew, you will first need to install Homebrew and the Xcode command line tools. Click on Spotlight (the magnifying glass icon in the upper right corner of the screen) and type "Terminal.app" to launch Terminal. Then, copy a command like "/bin/bash -c "$(curl -fsS ..." on the Homebrew page, paste it in the terminal and execute it. Then Homebrew itself and the Xcode command line Tools are installed.

At the time of writing, the Xcode command line tools are bundled with Python3. Still, if you want to use any version of Python, you can install Python from Homebrew by running the following command. You can install the latest stable version of Python3 at that point by running the following command:

brew install python3

However, when installing using Homebrew, please note that the installation destination is different from before. Until now, when Python was installed with Homebrew, it was installed under /usr/local/Cellar, but on M1 Mac, it is installed under /opt/homebrew/Cellar.

Processor typeInstall location
Intel Mac/usr/local/Cellar /python@3.x
M1 Mac/opt/homebrew/Cellar/python@3.x/

Compatibility with Intel Macs in Python

Note that some applications and libraries do not work after switching from Intel processors to Apple silicon. In fact, at the time of writing this manuscript, the SDK for Microsoft's speech recognition API, which was introduced in the 84th installment of the series, could not be installed. According to GitHub Issues, it is scheduled to be supported in mid-November, but those who use other libraries on M1-equipped Macs will have to wait until the libraries are supported.

By the way, installing Microsoft's Speech Recognition SDK currently requires a little ingenuity. The author introduced a container-type virtual environment "Docker", installed Ubuntu on the x86_64 platform, and installed the SDK on it to get it running.

Still, the M1 Mac has an Intel Mac emulation feature called Rosetta. If you install Rosetta, you will be able to use the command "arch". You can run commands for Intel Macs by running "arch -x86_64 (command)" on the command line. In other words, if you install and use x86_64 Python with Homebrew etc., you can run it as before.

As an alternative, you can install an external terminal such as iTerm and check "Open using Rosetta" (right-click the executable file and "Get Info") in the application settings to open the terminal. It can be used on par with Intel Mac. (However, in this case, it is necessary to devise the environment variable PATH so that commands for M1 and Intel are not mixed.)

However, Apple silicon support is progressing at a considerable speed. Therefore, in a little more time, the "M1 Mac won't work" will almost disappear. In the meantime, though, you can still use the Intel apps with Rosetta.

Let's benchmark with a simple self-made program

Next, let's create a simple program in Python and compare the operating speed with past Macbooks. Here, I created a function to find the N-th Fibonacci number as follows. Let's use this to compare the execution speed.

# Find the Nth Fibonacci number def fib(n):if n < 2: return 1 else: return fib(n-1) + fib(n-2)print(fib(35) )

I saved the above program as "fib.py" and executed the following command on the command line to measure the execution time.

time python3 fib.py

It's a simple program, but let's check the execution time with past Macbook Pro/Air. The model from two years ago is Air, so it's a little difficult to compare, but you can see that it's just twice as fast as the model from four years ago.

>< td>2x
ModelExecution speedHow many times faster
Macbook Pro (2017) 2.3GHz Intel Core i72.62s1x
Macbook Air (2019) 1.6GHz Intel Core i52.34 seconds1.11 times
Macbook Pro M1 Pro (2021)1.31 seconds

Next, let's check the performance of multithreading. Using the threading module Thread, I made a program that calls the above fib function continuously to find the Fibonacci numbers.

import threading# Find Nth Fibonacci number def fib(n):if n < 2: return 1 else: return fib(n-1) + fib(n-2)def print_fib(n ): print(fib(n)) for n in range(20, 35): t = threading.Thread(target=print_fib, args=[n]) t.start()

Save the above program as "fib2.py" and execute the following command to measure the execution time.

time python3 fib2.py

When I ran it, the following results were obtained. I was able to confirm that it is twice as fast as the Air two years ago and the Pro model four years ago.

>< td>2.06 times
ModelExecution speedHow many times faster
Macbook Pro (2017) 2.3GHz Intel Core i75.36s1x
Macbook Air (2019) 1.6GHz Intel Core i55.57 seconds0.96 times
Macbook Pro M1 Pro (2021)2.59 seconds

Although Apple's M1 Pro site claimed that the CPU was 3.7 times faster than the Core i7, the comparison machine is different. Or it seems that simple Python programs do not make much use of performance. Still, it is surprising that the performance has simply doubled after buying a new machine for the first time in several years.

Summary

The above is how to install Python on M1 Mac, compatibility with Intel Mac, and compared the execution speed by creating a simple program. When I compiled a work program, I honestly thought it was faster, but when I actually measured the execution speed of a simple program, I was able to confirm that it was quite fast, just as I was told. Also, personally, in addition to the good performance, I was happy that the Touch Bar was abolished and the [esc] key became larger. Either way, the M1 Mac certainly makes programming more fun.