DEV Community

Alex Spinov
Alex Spinov

Posted on

Mojo Has a Free API: Python Syntax With C++ Performance

What if Python was 68,000x faster? Not a typo. Mojo achieves C/C++ performance with Python syntax.

What Is Mojo?

Mojo is a programming language that's a superset of Python — your existing Python code runs in Mojo — but adds systems programming features that make it as fast as C.

# This is valid Mojo AND valid Python
def hello():
    print("Hello from Mojo!")

# But Mojo adds performance features
fn fast_hello():
    print("Hello from fast Mojo!")
Enter fullscreen mode Exit fullscreen mode

def works like Python (dynamic). fn is Mojo-specific (static, fast).

The Performance

# Mojo struct with SIMD operations
struct Matrix:
    var data: DTypePointer[DType.float64]
    var rows: Int
    var cols: Int

    fn __init__(inout self, rows: Int, cols: Int):
        self.rows = rows
        self.cols = cols
        self.data = DTypePointer[DType.float64].alloc(rows * cols)

    fn matmul(self, other: Matrix) -> Matrix:
        var result = Matrix(self.rows, other.cols)
        @parameter
        fn calc_row(i: Int):
            for j in range(other.cols):
                var sum: Float64 = 0
                @parameter
                fn dot[simd_width: Int](k: Int):
                    sum += (self.data.load<a href="i * self.cols + k">width=simd_width</a> *
                            other.data.load<a href="k * other.cols + j">width=simd_width</a>).reduce_add()
                vectorize[dot, simdwidthof[DType.float64]()](self.cols)
                result.data.store(i * other.cols + j, sum)
        parallelize[calc_row](self.rows)
        return result
Enter fullscreen mode Exit fullscreen mode

Auto-vectorization + parallelization in a Python-like language. This runs at C speed.

Python Compatibility

# Import any Python library
from python import Python

def use_numpy():
    np = Python.import_module("numpy")
    arr = np.array([1, 2, 3, 4, 5])
    print(np.mean(arr))  # Works!

    plt = Python.import_module("matplotlib.pyplot")
    plt.plot(arr)
    plt.show()
Enter fullscreen mode Exit fullscreen mode

All of PyPI works. NumPy, Pandas, TensorFlow, PyTorch — import and use them.

Why Mojo

  • 68,000x faster than Python on compute-intensive tasks
  • Python superset — existing Python code runs as-is
  • AI-first — designed for ML workloads (from Modular, the company behind MLIR)
  • Systems features — ownership, SIMD, manual memory management when needed
  • GPU programming — write GPU kernels in Mojo syntax

Building high-performance AI systems? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)