Hi everyone! In this post we are going to learn choosing the right and effective code pattern with some tools. First of all, we must know which programming language will be more usable and effective for our main purpose. For example, if you want to create a real-time system, C/C++ or Assembly will be a better choice for you (I prefer Assembly in most of the cases where I must do so). Imagine that we decided to use the C language. How can we know which code pattern or code snippet will be better?
This is a simple question to discuss. Let’s continue with our algorithm to find this answer.
- 1- Write a code snippet
- 2- Reverse it and look at the assembly code
- 3- Go to instruction set (depends on your hardware and requirements)
- 4- Count clock cycle
You can use the debugger that you want to use, the Godbolt website, or another reverse engineering tool. We will use Godbolt in this example with a specific version of the compiler and system. You can change it and try other systems yourself. Please try to understand the methodology, not just this example.
- Code 1 → a++
- Code 2 → a=a+1
- Code 3 → a+=1
My Settings:
Example 1:
Example 2:
Example 3:
As shown, you can use all of these examples with the same clock cycle because they will all point to the same assembly code in low-level language.
I’m hoping you got the idea behind the try-fault strategy and how it might help you discover a good solution to a coding issue.
Thanks for your time and happy coding 🙂