Last Updated: February 29, 2016
·
14.69K
· ccubed

Identifying Memory and CPU Intensive Code

Continuing from my previous pro tip, really more of an opinion, on Memory and CPU Intensive applications and the disregard for the concept that is prevalent, this is meant to be a quick introduction into identifying Memory and CPU Intensive Code. Before we begin however, let's define a couple of things so we know how I'm using the words I'm using.

Code: Any set of instructions written in a programming language

CPU Intensive: Any code that results in the work being primarily pushed onto the CPU

Memory Intensive: Any code that results in the work being primarily pushed onto the Memory

Memory Intensive seems like a weird definition and I don't particularly like the way it's worded. However, I don't know of a better way to put it in this context. It's not that the actual work is done by your Memory alone, however, it's meant to represent the fact that the impact on the CPU is either nothing or minimal.

At the moment, let's examine a quick example for both cases. These will be simple examples, but they do the job of introducing you to the concept and explaining why they're one or the other.

CPU Intensive Code
```C++

include <time.h>

int main()
{
time_t timer;
time(&timer);
}
```

The code above is very CPU intensive. The reason being that the way time actually works is by getting the CPU to calculate the number of seconds since the Epoch which at the moment is January 1, 2000. As you can imagine, that's a fairly intensive mathematical calculation.

Memory Intensive Code
```C++
int main()
{

int x = 2;
int y;
y=x;

}
```

The code above is Memory Intensive. In this case, all of the work is pushed onto the Memory because all we're doing is assignment. This won't cause explosions in memory usage, but that doesn't make it any less of an example of Memory Intensive Code.

Which brings me to my next point. The idea of Memory and CPU Intensive Code is largely relative. That is to say that not all examples of Memory or CPU Intensive Code are going to instantly shoot your Memory or CPU usage through the roof. However, that doesn't mean the designation doesn't apply and in general, most apps that we make are going to use chunks of Memory that are not small and take CPU work that is not simple. The example above of CPU Intensive Code for instance will instantly shoot one of your cores up to 100% usage. Depending on your computer and processor, you may instead see an instant jump of equal amounts across all your cores as it distributes the work. Doing the time call in a for loop will keep your CPU usage at 100% until the for loop ends - again, this may manifest as distributed across your processors if you have more cores.

Sometimes it can be harder to tell whether code is Memory or CPU intensive, so let's look at a different example.

int main()
{
int x[10];

   for( int i(0); i < 10;i++)
      x[i]=1;

   for( int i(0);i < 10;i++)
      for( int y(0);y<10;y++)
         x[y] = x[i] + 1
}

So what about the above? Well, let's see.

int x[10];

This is a memory operation obviously. We are creating an array of integers.

for(int i(0);i<10;i++)
   x[i]=1;

This is a combination of Memory and CPU. The for loop is CPU Intensive but the assignment is Memory Intensive.

for( int i(0);i < 10;i++)
   for( int y(0);y<10;y++)
      x[y] = x[i] + 1

This is a little more confusing right? Technically the two loops require calculations as well as the assignment. We've already had an additional for loop as well and from the looks of things, we have more work that is pushed back onto the CPU than work that is done in memory. Again, this is a system based on relativity so in actuality none of this would cause a strain on any computer. However, that doesn't make the operation any less CPU Intensive.

Now that you've had a basic introduction to the concept of Memory and CPU Intensive Code, I hope that you start to apply it to your projects. Think about things that could be done better in a Memory Intensive operation than a CPU Intensive operation. Think about ways in which leveraging the fact that you can decide which is used can help you distribute the process across resources which can ultimately speed up the program.

In the next pro tip tutorial, we'll get into code from an open source project named PCSX2 and decipher some of their code to determine CPU and Memory Intensive operations.