Bifalgorithm post

Why in most programming languages is adding an array of integers sequentially much faster than adding them randomly? This seems strange since you add the same number of elements either way, but the reason for it is pretty fascinating. When the CPU accesses an element in the array, it actually copies a block of consecutive elements from RAM into something called the L1 cache. This is a special memory layer inside the CPU itself and is 50 times faster to access. Now, it sounds powerful, but if you're accessing elements in a random order, then it isn't very useful. Say the CPU already loaded the 5th element from a previous read and now needs a 2nd element. Since 2 isn't in the cache, the CPU has to go all the way back to RAM to retrieve a new block of data. It'll do this often since it's jumping around the array a lot and therefore be very slow. But if the CPU is accessing data sequentially, we have a different outcome. After the CPU grabs the first element, it can get the second, third and fourth elements straight from the cache, and as a result, execute very quickly.

Why in most programming languages is adding an array of integers sequentially much faster than adding them randomly? This seems strange since you add the same…

Ideas in this flowchart

  • Array Addition Speed in Programming
  • Sequential vs Random Addition
  • Sequential Addition
  • Random Addition
  • L1 Cache Mechanism
  • L1 Cache Description
  • Sequential Access Benefit
  • Random Access Drawback
  • Block Copying to L1 Cache
  • Cache Miss in Random Access
Feed/

Why in most programming languages is adding an array of integers sequentially much faster than adding them randomly? This seems strange since you add the same number of elements either way, but the reason for it is pretty fascinating. When the CPU accesses an element in the array, it actually copies a block of consecutive elements from RAM into something called the L1 cache. This is a special memory layer inside the CPU itself and is 50 times faster to access. Now, it sounds powerful, but if you're accessing elements in a random order, then it isn't very useful. Say the CPU already loaded the 5th element from a previous read and now needs a 2nd element. Since 2 isn't in the cache, the CPU has to go all the way back to RAM to retrieve a new block of data. It'll do this often since it's jumping around the array a lot and therefore be very slow. But if the CPU is accessing data sequentially, we have a different outcome. After the CPU grabs the first element, it can get the second, third and fourth elements straight from the cache, and as a result, execute very quickly.

"Why in most programming languages is adding an array of integers sequentially"

👍
0
0 comments0 shares

Sign in to view and post comments.