<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg" -->

---
title: The Two-Line Code Swap That Made My Program 23% Faster
description: A C++ performance experiment demonstrates how swapping the order of two nested loop variables in a matrix multiplication algorithm yields a ~23% speedup. The...
canonical: https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The Two-Line Code Swap That Made My Program 23% Faster | daily.dev
og:description: A C++ performance experiment demonstrates how swapping the order of two nested loop variables in a matrix multiplication algorithm yields a ~23% speedup. The...
og:url: https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg
og:image: https://api.daily.dev/og/posts/cjLJhR2dg.png
og:image:alt: The Two-Line Code Swap That Made My Program 23% Faster
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The Two-Line Code Swap That Made My Program 23% Faster

**[Medium](https://daily.dev/sources/medium_js)** · 5 min read · 3 upvotes · 0 comments

## Summary

A C++ performance experiment demonstrates how swapping the order of two nested loop variables in a matrix multiplication algorithm yields a ~23% speedup. The root cause is CPU cache behavior: the original i-j-k loop order caused the processor to read memory vertically, resulting in frequent cache misses. Switching to i-k-j order aligns memory access with how data is stored in RAM (row-major order), enabling the CPU's L1 cache to be used efficiently. The concept illustrated is spatial locality — reading data sequentially as the hardware was designed to do.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@vaibhavkhokha/the-two-line-code-swap-that-made-my-program-23-faster-2e9110732bfd>

## Questions this post answers

### Why does swapping the loop order in matrix multiplication from i-j-k to i-k-j make it faster?

Swapping to i-k-j order makes the innermost loop iterate over the column index, so the computer reads both matrices horizontally in memory rather than jumping down columns. This aligns access with how the CPU fetches contiguous chunks into its L1 cache, drastically reducing cache misses. In one test on a 1000x1000 matrix multiplication, this reordering alone cut runtime from 0.88 seconds to 0.68 seconds, roughly a 23% speedup.

_Developers chasing cache-friendly performance tricks can find similar deep-dive breakdowns on daily.dev._

### What is spatial locality and why does it matter for CPU performance?

Spatial locality is the principle that data used together should be stored together in memory and accessed sequentially, since CPUs fetch data in contiguous horizontal chunks into the fast L1 cache. When code accesses memory sequentially instead of jumping around, it uses every value fetched into cache before requesting more, avoiding costly cache misses that force the CPU to wait on slower RAM.

_daily.dev surfaces practical explanations like this for engineers optimizing low-level code performance._

## Similar posts on daily.dev

- [When compilers surprise you — Matt Godbolt’s blog](https://daily.dev/posts/when-compilers-surprise-you-matt-godbolt-s-blog-3c6va0gvq) · Hacker News · 1 upvotes · 0 comments
- [How to Write High-Performance Code](https://daily.dev/posts/how-to-write-high-performance-code-1e303rwjv) · ByteByteGo · 14 upvotes · 0 comments

---

Tags: [#c++](https://daily.dev/tags/c++)

[View this post on daily.dev](https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The Two-Line Code Swap That Made My Program 23% Faster","url":"https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg"},"datePublished":"2026-07-16T19:34:06.749Z","dateModified":"2026-09-13T21:29:31.213Z","description":"A C++ performance experiment demonstrates how swapping the order of two nested loop variables in a matrix multiplication algorithm yields a ~23% speedup. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/cfc24f4d8ab453ea75768f0fa23d7a3a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/cfc24f4d8ab453ea75768f0fa23d7a3a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"c++","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"The Two-Line Code Swap That Made My Program 23% Faster"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-two-line-code-swap-that-made-my-program-23-faster-cjljhr2dg#faq","mainEntity":[{"@type":"Question","name":"Why does swapping the loop order in matrix multiplication from i-j-k to i-k-j make it faster?","acceptedAnswer":{"@type":"Answer","text":"Swapping to i-k-j order makes the innermost loop iterate over the column index, so the computer reads both matrices horizontally in memory rather than jumping down columns. This aligns access with how the CPU fetches contiguous chunks into its L1 cache, drastically reducing cache misses. In one test on a 1000x1000 matrix multiplication, this reordering alone cut runtime from 0.88 seconds to 0.68 seconds, roughly a 23% speedup. Developers chasing cache-friendly performance tricks can find similar deep-dive breakdowns on daily.dev."}},{"@type":"Question","name":"What is spatial locality and why does it matter for CPU performance?","acceptedAnswer":{"@type":"Answer","text":"Spatial locality is the principle that data used together should be stored together in memory and accessed sequentially, since CPUs fetch data in contiguous horizontal chunks into the fast L1 cache. When code accesses memory sequentially instead of jumping around, it uses every value fetched into cache before requesting more, avoiding costly cache misses that force the CPU to wait on slower RAM. daily.dev surfaces practical explanations like this for engineers optimizing low-level code performance."}}]}
```

