Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loop Order in 2D Arrays | The Case of the Slow Matrix: A Developer’s Mystery 🔍
Apr 7, 2025
191 views
Written by Prashant Basnet
<section class="bg-white dark:bg-gray-900 px-4 py-8 max-w-2xl mx-auto text-gray-800 dark:text-gray-200">
<h1 class="text-2xl sm:text-3xl font-signature italic font-semibold text-center mb-4">
👋 Welcome — You’ve Landed on My Signature Page
</h1>
<p class="text-base sm:text-lg mb-4">
Hey, I’m <strong class="text-black dark:text-white">Prashant Basnet</strong> — software developmemt engineer at
<a href="https://unisala.com" class="text-indigo-600 dark:text-indigo-400 underline hover:no-underline" target="_blank" rel="noopener noreferrer">
Unisala.com
</a>.
</p>
<p class="text-base sm:text-lg mb-6">
You’re viewing my <strong>Signature</strong>, a digital space where I share what I’m learning, building, and reflecting on, all in one place.
</p>
<div class="border-l-4 border-indigo-400 dark:border-indigo-500 pl-4 italic mb-6 text-sm sm:text-base text-gray-700 dark:text-gray-400">
📍 Found this page via LinkedIn, my personal site, or a shared link?
<br />
This isn’t a traditional portfolio. It’s my public digital notebook where I document useful ideas, experiments, and lessons I’ve learned as I build.
</div>
<h2 class="text-lg font-semibold mb-2">What You’ll Find Here:</h2>
<ul class="list-disc list-inside space-y-1 text-sm sm:text-base">
<li>✍️ Thoughts on algorithms, systems, and software design</li>
<li>🧠 Insights from building at Unisala</li>
<li>🔗 Direct links to everything I’ve published on Unisala</li>
</ul>
</section>
Chapter 1: The Mysterious Slowdown
Alex, a junior engineer at a fast-paced AI startup, was proud of their latest code. It processed massive datasets for a new recommendation engine, simple matrix multiplications, nothing fancy.
But when they ran it, the program crawled.
Chapter 2: The Two Loops
Alex reviewed the code. Two versions, nearly identical:
Version A (Column-First):
Version B (Row-First):
Same math. Same number of operations. Totally different performance.
Then why care about loop order?
Chapter 3: The Clue in the Cache
She drew a diagram:
The CPU cache hates random access.
Chapter 4: The Fix
The code now processed millions of entries in seconds, not minutes.
Chapter 5: The Bigger Picture
Dr. Lee shared stories:
Epilogue: The Lesson
Alex’s code shipped on time. The team celebrated. But the real win?
A new mindset:
If performance really matters, flatten your 2D array:
Mystery solved. Performance unlocked. You control memory layout and access patterns precisely.
#Programming #Performance #DevMysteries