Stack vs malloc: real-world benchmark shows 2–6x difference
<p>Usually, we assume that malloc is fast—and in most cases it is. <br> However, sometimes "reasonable" code can lead to very unreasonable performance.</p> <p>In a previous post, I looked at using stack-based allocation (VLA / fixed-size) for temporary data, and another on estimating available stack space to use it safely.</p> <p>This time I wanted to measure the actual impact in a realistic workload.</p> <h3> Full Article (Medium - no paywall): </h3> <p><a href="https://blog.stackademic.com/temporary-memory-isnt-free-allocation-strategies-and-their-hidden-costs-159247f7f856" rel="noopener noreferrer">Stack vs malloc: real-world benchmark shows 2–6x difference</a></p> <p>I built a benchmark based on a loan portfolio PV calculation, where each loan creates several temporary arrays (thousand
Usually, we assume that malloc is fast—and in most cases it is. However, sometimes "reasonable" code can lead to very unreasonable performance.
In a previous post, I looked at using stack-based allocation (VLA / fixed-size) for temporary data, and another on estimating available stack space to use it safely.
This time I wanted to measure the actual impact in a realistic workload.
Full Article (Medium - no paywall):
Stack vs malloc: real-world benchmark shows 2–6x difference
I built a benchmark based on a loan portfolio PV calculation, where each loan creates several temporary arrays (thousands of elements each). This is fairly typical code-clean, modular, nothing unusual.
I compared:
-
stack allocation (VLA)
-
heap per-loan (malloc/free)
-
heap reuse
-
static (baseline)
Results:
-
stack allocation stays very close to optimal
-
heap per-loan can be ~2.5x slower (glibc) and up to ~6x slower (musl)
-
even optimized allocators show pattern-dependent behavior
The main takeaway for me: allocation cost is usually hidden—but once it's in the hot path, it really matters.
Curious how others approach temporary workspace in performance-sensitive code.
DEV Community
https://dev.to/yairlenga/stack-vs-malloc-real-world-benchmark-shows-2-6x-difference-4601Sign in to highlight and annotate this article

Conversation starters
Daily AI Digest
Get the top 5 AI stories delivered to your inbox every morning.
More about
benchmarkavailable
dark ilan
The second time Vellam uncovers the conspiracy underlying all of society, he approaches a Keeper. Some of the difference is convenience. Since Vellam reported that he’d found out about the first conspiracy, he’s lived in the secret AI research laboratory at the Basement of the World, and Keepers are much easier to come by than when he was a quality control inspector for cheese. But Vellam is honest with himself. If he were making progress, he’d never tell the Keepers no matter how convenient they were, not even if they lined his front walkway every morning to beg him for a scrap of his current intellectual project. He’d sat on his insight about artificial general intelligence for two years before he decided that he preferred isolation to another day of cheese inspection. No, the only reaso

Apple iOS 26.5 public beta is now available
Apple has released the first public beta for iOS 26.5, just a few days after the beta for developers came out. One of the biggest changes the new operating system brings is the “ Suggested Places ” feature in Apple Maps. It will show you trending places to visit, such as restaurants and other establishments, near your location or based on your search history. You can see Suggested Places when you tap on the search bar in the Maps app. iOS 26.5 beta also will also come with notifications that the company will be putting ads inside Maps. Apple confirmed in March that it was going to expand its ads outside of the App Store and Apple News apps. The ads you see will be based on your location, the search terms you’ve used and what you’re looking up on Maps. They will show up at the top of your s
Knowledge Map
Connected Articles — Knowledge Graph
This article is connected to other articles through shared AI topics and tags.
More in Models

National Robotics Week — Latest Physical AI Research, Breakthroughs and Resources
This National Robotics Week, NVIDIA is highlighting the breakthroughs that are bringing AI into the physical world — as well as the growing wave of robots transforming industries, from agricultural and manufacturing to energy and beyond. Advancements in robot learning, simulation and foundation models are accelerating development, enabling robots to move from training in virtual [ ]





Discussion
Sign in to join the discussion
No comments yet — be the first to share your thoughts!