Mojo Programming Language — 68000x Faster than Python: Programming in Mojo — Part II

Shriram Sivanandhan
3 min readSep 15, 2023

Mojo Programming Language combines the usability of Python but with the performance of C, C++ and Rust. Mojo utilizes next-generation compiler technologies with features like caching, multithreading and cloud distribution technologies and also, Auto-Tuning and compile-time Metaprogramming features allows to write code for several hardware.

Mojo Programming Language 68000x faster than Python:

Mojo incorporates advanced features and achieves High Performance. As previously mentioned, Mojo claims to be 35000x faster than Python, but recently Modular-Mojo Development Team had claimed that Mojo is 68000x faster than Python.

Why Mojo achieves 68000x speedup instead of 35000x speedup than Python. Earlier Mojo claims 35000x faster than Python by executing/evaluating Mandelbrot Algorithm on r7iz.metal-16xl Intel Xeon Instance. But recently the Modular-Mojo Development Team claimed that Mojo is 68000x faster than Python by executing/evaluating Mandelbrot Algorithm on h3-standard-88 Intel Xeon Instance. Upgrade in number of cores and faster clock speed generally results in Mandelbrot Algorithm speedup, Oversubscription has been used to resolve the load imbalance due to Parallelism and Mojo has achieved 68000x faster than Python on h3-standard-88 Intel Xeon Instance. This Hardware Scaling does not require Mojo Code to be changed, also Program Optimization is also not done across the Instance types, but we can get the Performance Improvement.

Previously, Mojo claimed to be 35000x faster than Python by executing/evaluating Mandelbrot Algorithm on r7iz.metal-16xl Intel Xeon Instance. Source: https://www.modular.com/mojo
Recently, the Modular-Mojo Development Team claimed that Mojo is 68000x faster than Python by executing/evaluating Mandelbrot Algorithm on h3-standard-88 Intel Xeon Instance. Source: https://www.modular.com/mojo

Programming in Mojo:

In this Blog, Programming in Mojo is done using Mojo Playground.

Basics of Mojo Programming:

Mojo Programming Language is a Compiled Language and its performance and memory-safety features are due to the fact Mojo is a Compiled Language. Mojo Programming Code can be Ahead-Of-Time (AOT) or Just-In-Time (JIT) compiled.

If we work in REPL Environment or Jupyter Notebook (Mojo Playground JupyterHub Environment), we can run Top-Level code like Python as mentioned below,

print(“Hello Mojo!!!”)

Output:

Hello Mojo!!!

Printing Hello Mojo!!! in Mojo Playground

But, whenever we write Mojo Code in .mojo / .🔥 file we can’t run Top-Level code like we run in REPL Environment or Jupyter Notebook (Mojo Playground JupyterHub Environment), all the Mojo code must be enclosed in a function or struct.

Similar to other compiled programming languages, Mojo Programs requires main() function as the entry point to the program.

Example Mojo Program:

Example Mojo Program

Function declaration in Mojo Programming Language will be discussed later. Like Python, function can be declared in Mojo Programming Language using def keyword and also we can use fn keyword. Both fn and def keywords are used to declare functions in Mojo Programming Language but using functions declared using fn keyword behaves differently and it will be discussed later. In the above example we had used function declaration using fn keyword.

Hence, Top-Level code can be executed in REPL Environment or Jupyter Notebook (Mojo Playground JupyterHub Environment). But Mojo Code in .mojo / .🔥 file can’t run Top-Level code and all the Mojo code must be enclosed in a function or struct.

We had seen about the overachievement of Mojo programming Language Performance than Python — 68000x faster than Python Programming Language by executing/evaluating Mandelbrot Algorithm on h3-standard-88 Intel Xeon Instance. Then we saw some Basics of Mojo Programming Language. Programming in Mojo and Mojo Programming concepts will be discussed further.

Thankyou for reading this blog on Mojo Programming Language — 68000x Faster than Python: Programming in Mojo — Part II.

Reference:

https://www.modular.com/mojo

https://docs.modular.com/mojo/

--

--