Value In Brief
All You Need To Know Explained In Brief

Code Optimization Tips With Examples

In this post, I will show you how to optimize your code with examples. Optimization for speed, readability and scalability. Almost all code snippets will be written in Go programming languages, but still .. all those tips are applicable in other programming languages like PHP, Javascript, Java, Kotlin, .. etc. Calculate it once calculating things again and again is horrible. It consumes CPU time more than actually needed. So we should stop re-calculating things.

How to store float values efficiently in Laravel Eloquent ?

If you want to store price in dollars such as 4.99, just store it as integer not float in database (499). And in your model add this function. public function getPriceAttribute(){ return floatval(number_format($this->attributes["price"] / 100, 2)); } When you get the price, it is 4.99 , but it is stored as 499. This saves a huge space when you have more than 10k records in database. The optimization aspect is to store double float values as integers in database.

Why built-in functions in python are faster ?

Because Python is sitting of the shoulders of giants. Built-in functions are written in C and C++ under the hood. C and C++ languages are efficient and fast, so their functions will be faster than the functions you write in Python. So it is recommended to use built-in functions instead of creating new ones.

Why is n++ faster than n=n+1?

The answer is related to the old CISC cpu instructions and the way how C (or any programming language) is compiled to binary code. In almost every CISC CPU there is a special “add” instruction that is named INC (or DEC) and that instruction increments (or decrements) by 1 the value of a register (or memory). If you use another instruction for addition you have to add both operands you want to sum.