What is the fastest way to transpose a matrix in C?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Matrix transpose performance is usually limited by memory access patterns, not arithmetic. A naive element-by-element transpose works correctly but can be slow on large matrices due to cache misses. In C, blocked transpose and careful loop ordering are typically the fastest practical approaches.
Baseline Transpose Implementation
Start with a simple reference implementation for correctness.
This is easy to verify, but memory writes to dst are strided and cache-unfriendly.
Improve Cache Locality with Blocking
Blocked transpose processes small tiles that fit better in cache.
Blocking reduces cache thrashing and usually gives substantial speedups for large matrices.
In-Place Transpose for Square Matrices
If the matrix is square and stored in one buffer, swap symmetric elements.
This avoids extra output buffer allocation, but works only for square shapes.
Benchmarking Methodology
Always benchmark with realistic matrix sizes and compiler optimization flags.
Measure multiple runs, warm caches, and report median timing. Performance conclusions from tiny matrices are often misleading.
Additional Optimization Ideas
For high-end workloads, consider vectorized intrinsics and parallelization with OpenMP. However, blocked scalar code is often a strong baseline and easier to maintain.
If using parallel loops, ensure each thread works on separate blocks to reduce false sharing.
When matrix sizes are known at compile time, specialized kernels can outperform generic loops.
Practical Benchmark Harness in C
A small benchmark harness helps compare naive and blocked versions on your hardware.
Use large matrices and multiple repetitions. Record median times and verify output correctness after each run to ensure optimizations do not introduce indexing bugs.
Common Pitfalls
A common pitfall is focusing only on algorithmic complexity. Both naive and blocked transpose are linear in element count, yet cache behavior changes real speed dramatically.
Another issue is choosing block size without profiling. Optimal tile size depends on cache hierarchy and data type.
Developers also benchmark debug builds and draw incorrect conclusions.
A final problem is incorrect index math for non-square matrices, causing silent data corruption.
Summary
- Transpose speed is mostly driven by memory access locality.
- Naive loops are correct but often slow for large matrices.
- Blocked transpose improves cache usage and practical throughput.
- In-place transpose is efficient for square matrices only.
- Benchmark with optimized builds and representative matrix sizes.
Related reading
- What is the fastest/most efficient way to find the position of the highest set bit msb in an integer in C?
- What is the Google Map zoom algorithm?
- What is the idea behind scaling an image using Lanczos?
- what is the key difference between multipaxos and basic paxos protocol
- What is the intended use of the optional else clause of the try statement in Python?
- What is the LIMIT clause alternative in JPQL?
- What is the meaning of 'const' at the end of a member function declaration?
- What is the optimization level g you use while comparing two different algorithms written in C?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.