Skip to content

Compare execution time of matrix multiplication using C, pure Python, and NumPy

Notifications You must be signed in to change notification settings

TrNguyenMQuan/matrix-multiplication-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Matrix Multiplication Performance Comparison

Project Overview

This project benchmarks and compares the runtime performance of three different matrix multiplication implementations to understand the trade-offs between language choice and optimization:

  • C Implementation - Low-level with -O3 optimization
  • Pure Python - Native nested lists with triple loops
  • NumPy - Optimized C/Fortran backend (BLAS/LAPACK)

Goal: Demonstrate why NumPy is essential for numerical computing in Python by measuring execution times across matrix sizes from 64×64 to 2048×2048.


Project Structure

Homework 1/
├── Homework 1.ipynb        # Main notebook with full analysis &visualizations
├── matrix_mult.c           # C implementation source code
└── README.md               # This file

File Descriptions:

  • Homework 1.ipynb: Complete benchmark suite with code, results, plots, and analysis
  • matrix_mult.c: C implementation using triple nested loops with dynamic memory allocation

Installation

Prerequisites

  • Python 3.x
  • GCC Compiler
  • Jupyter Notebook

Step 1: Install Python Dependencies

pip install numpy pandas matplotlib jupyter

Step 2: Install GCC (if not already installed)

  • Windows: Install MinGW or use WSL
  • Linux: sudo apt-get install gcc
  • macOS: xcode-select --install

Step 3: Compile C Program

gcc -O3 matrix_mult.c -o matrix_mult

How to Run

Run Full Benchmark

Open the Jupyter notebook and execute all cells:

jupyter notebook "Homework 1.ipynb"

This will:

  1. Compile and test C implementation
  2. Run Pure Python benchmarks
  3. Run NumPy benchmarks
  4. Generate performance comparison plots
  5. Display detailed analysis

Results & Performance Analysis

Performance Summary (1024×1024 Matrix)

Implementation Execution Time Speedup vs Python
NumPy 39.5 ms 5,680× faster
C 3,154.8 ms 71× faster
Pure Python 224,311.3 ms Baseline (1×)

Key Findings

NumPy dominates - Even beats optimized C code (~80× faster)
C is fast - Significantly outperforms Pure Python
Pure Python is impractical - Orders of magnitude slower for large matrices

Visualization Examples

The notebook generates two main plots:

  1. Execution Time vs Matrix Size (log-log scale)

    • Demonstrates O(N³) complexity
    • Shows exponential performance gaps
  2. Speedup Factor Analysis

    • NumPy vs Pure Python speedup
    • C vs Pure Python speedup
    • NumPy vs C speedup

Note: Both charts will be generated and displayed inline within the Homework 1.ipynb notebook after executing the corresponding code cells.

Why These Results?

Aspect Pure Python C NumPy
Execution Interpreted Compiled Compiled (C/Fortran)
Typing Dynamic Static Static
Optimization None -O3 flag BLAS/LAPACK + SIMD
Memory Scattered pointers Contiguous Cache-optimized blocks

NumPy's secret: Uses decades-optimized BLAS/LAPACK libraries with SIMD vectorization and intelligent cache blocking - hard to replicate manually.


Author & Contributions

Author: Student name: 23120342 - Student ID: 23120342
Course: Programming for Data Science
Assignment: Homework 1 - Performance Benchmarking

Acknowledgments

  • NumPy development team for the optimized library
  • BLAS/LAPACK developers for foundational algorithms
  • Course instructors for guidance

Contributions: This is an educational project. Feel free to fork and experiment with different matrix sizes or algorithms!


License

This project is created for educational purposes as part of a Programming for Data Science course assignment.

Usage: Free to use for learning and reference. Please cite if used in academic work.


Additional Resources


Key Takeaway: Always use NumPy for numerical computing in Python - it's not optional, it's essential!

About

Compare execution time of matrix multiplication using C, pure Python, and NumPy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published