matrix multiplication algorithm time complexity
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The time complexity of matrix multiplication depends on which algorithm you mean and on the matrix shapes involved. For the standard textbook algorithm multiplying an m x n matrix by an n x p matrix, the time complexity is O(mnp).
When people quote O(n^3), they are talking about the special case where all matrices are square and have the same dimension n. That is the starting point, not the only answer.
Standard Matrix Multiplication
Suppose matrix A has shape m x n and matrix B has shape n x p. Their product C has shape m x p, and each entry is a dot product of length n.
A direct implementation looks like this:
The three nested loops explain the complexity directly:
- '
mchoices for the row' - '
pchoices for the column' - '
nmultiplications and additions per output entry'
So the running time is O(mnp).
Why Square Matrices Become O(n^3)
If m = n = p, then the formula becomes:
That is why introductory algorithms courses often summarize matrix multiplication as cubic time.
For example, multiplying two 1000 x 1000 matrices with the naive algorithm scales on the order of a billion scalar multiply-add style operations.
Faster Algorithms Exist
Researchers found algorithms that beat the cubic bound for large square matrices. The most famous practical theoretical milestone is Strassen's algorithm, which reduces the exponent to about 2.807.
The idea is to split matrices into blocks and reduce the number of recursive multiplications.
There are also even faster asymptotic algorithms with smaller exponents, but they are usually not what engineers mean in day-to-day software work because the constant factors and implementation complexity are high.
Why the Naive Complexity Still Matters
Even though faster asymptotic algorithms exist, the standard O(mnp) cost is still the most useful answer in many contexts because:
- it matches simple implementations
- many libraries switch algorithms internally based on size
- practical performance depends heavily on cache layout, blocking, and hardware acceleration
So a library may run much faster than a naive triple loop while still fundamentally being discussed against the classical cubic baseline.
Theoretical Complexity Versus Real Performance
Time complexity is not the same as wall-clock speed. A theoretically faster algorithm can lose in practice on small or medium matrices because of overhead.
That is why optimized BLAS libraries, GPU kernels, and blocked algorithms matter so much in practice. They improve performance dramatically even when you still conceptually start from matrix multiplication's classical structure.
Do Not Confuse This with Matrix Chain Multiplication
There is a different problem called matrix chain multiplication that asks for the best parenthesization of many matrix products. That problem is about minimizing the total number of scalar operations across a whole chain.
It is not the same as asking for the time complexity of multiplying one matrix by another.
Common Pitfalls
- Saying matrix multiplication is always
O(n^3)without mentioning the rectangularO(mnp)form. - Assuming asymptotically faster algorithms are always faster in practical code.
- Confusing the complexity of one multiplication with the separate matrix chain ordering problem.
- Ignoring hardware and memory effects when comparing real implementations.
- Forgetting that output size itself is
m x p, so even writing the result has a cost tied to matrix dimensions.
Summary
- Standard multiplication of an
m x nmatrix by ann x pmatrix takesO(mnp)time. - For square
n x nmatrices, that becomesO(n^3). - Faster algorithms such as Strassen reduce the exponent for large square matrices.
- Practical runtime depends on implementation details, not only asymptotic notation.
- Use
O(mnp)when discussing general shapes andO(n^3)for the common square-matrix shorthand.

