# Profiling vs Benchmarking 101

> 𝐏𝐫𝐞𝐦𝐚𝐭𝐮𝐫𝐞 𝐨𝐩𝐭𝐢𝐦𝐢𝐳𝐚𝐭𝐢𝐨𝐧 𝐢𝐬 𝐭𝐡𝐞 𝐫𝐨𝐨𝐭 𝐨𝐟 𝐚𝐥𝐥 𝐞𝐯𝐢𝐥 - Donald Knuth

As a developer, we often dive into our codebase, thinking, "Hey, let's tweak this bit or give that part a little upgrade!" But, man, even the simplest-looking stuff can have a whole party going on behind the scenes. Modern frameworks? They're like magicians, hiding all the tricky bits and making everything seem easy-peasy. Crazy, right?

Before diving in headfirst, take a moment and ask yourself:

### 𝐀𝐫𝐞 𝐘𝐨𝐮 𝐏𝐫𝐨𝐟𝐢𝐥𝐢𝐧𝐠, 𝐁𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤𝐢𝐧𝐠, 𝐨𝐫 𝐉𝐮𝐬𝐭 𝐆𝐮𝐞𝐬𝐬𝐢𝐧𝐠?

Benchmarking is the process of measuring the performance of specific parts of your program, typically to compare before-and-after performance or to compare against other solutions. It gives you quantitative data about how fast they execute or how many resources they consume. When we benchmark, we take two competing pieces of code and compare them. It could be as simple as a one-liner, or as complex as an entire web framework.
At the end of the task, we come up with a single metric - a score. We use the score to compare the two competing options.

Profiling is the process of analyzing a piece of code to understand where it spends its time, which helps identify bottlenecks or performance hotspots.
Profiling doesn’t usually produce a comparable “score” at the end with which to measure these alternatives, either. But that’s not to say profiling is useless - it can tell us a lot of valuable things, like what percentage of CPU time was used where, where memory was allocated, and what lines of code are important and which ones aren’t.

![example](https://pbs.twimg.com/media/F772DcmWcAAjeOE?format=jpg&name=large)

#### 𝐖𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 𝐛𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤𝐢𝐧𝐠:
- Use benchmarking when you have specific pieces of code and you want to compare their performance.

- When you're trying to compare different algorithms or strategies to solve a particular problem, and you want to know which is the fastest.

#### 𝐖𝐡𝐞𝐧 𝐭𝐨 𝐮𝐬𝐞 𝐩𝐫𝐨𝐟𝐢𝐥𝐢𝐧𝐠:
- Use profiling when you know there's a performance issue, but you're unsure where the problem lies.

- When you're looking for a holistic view of your application's performance and want to understand which parts are consuming the most time or resources.

A good rule of thumb is to remind yourself every time "𝑰 𝒘𝒊𝒍𝒍 𝒏𝒐𝒕 𝒐𝒑𝒕𝒊𝒎𝒊𝒛𝒆 𝒂𝒏𝒚𝒕𝒉𝒊𝒏𝒈 𝒊𝒏 𝒎𝒚 𝒂𝒑𝒑𝒍𝒊𝒄𝒂𝒕𝒊𝒐𝒏 𝒖𝒏𝒕𝒊𝒍 𝒎𝒚 𝒎𝒆𝒕𝒓𝒊𝒄𝒔 𝒕𝒆𝒍𝒍 𝒎𝒆 𝒔𝒐."
