5 Common sense principles for a better code performance

Adel Ghamri
5 min readSep 23, 2021

--

Time is absolutely the most important resource we have. Unlike other resources, it is unrecoverable, unstoppable, unrenewable. Performance, in several domains, is measured againts time. How fast do you do things ? hopefully well but not necessarily. Many adages favour times over quality…Go fast and break things.

When it comes to code, the speed of execution is critical. If your app is slow, user will undoubtly skip it. Big web players (especially e-commerce retailers…) fights for ms because it worth millions in revenues.

Performance is also relative. Nowdays, the standard of reactivity is high. In the 90’s we could wait several minutes for a page to load. Today, a first print that takes more than 2 seconds is same as 404 not found. Maybe the later is better, because it generally show up faster.

I will only talk about code, although performance envolves other aspects like network, hardware…etc. I will lay out few common sense principles with parallels made to daily life situations.

1. Do it once and for all

This is about not repeating a procedure whose output is not about to change while it is used several times within a frame or a process. Technically speaking this is achieved through caching (also called memoization) or simply storing result into a variable (this is also a simple form of caching). However, great caution shall be taken to ensure the freshness. Cache invalidation is one of the hardest problem in computer science. Also, retrieving the cached value sometimes cost far more than re-calculating it. In some cases, CPU calculation is faster than memory access or searchs operation into big hashed tables, so you had better to recalculate.

This general rule of thumb shall be checked in the context but it is generally valid.

Imagine an Excel sheet where you calculate:

A1=SUMPRODUCT(BigRangeOfCells) ; B1=A1*A1; C1=B1*A1

If a tiny cell in the BigRangeOfCells changes, Excel refreshes the value of A1, B1 then C1.

How many times shall Excel calculate the formula in A1? Actually it is called 6 times. 1 in A1, 2 in B1, 3 in C1.

Will the value of A1 formula changes several times within the refresh frame? NO. So, A1 shall be evaluated once and memoized until the next change. If you are a spreadsheet user, you daily life is easier thanks to caching. Did you know it?

2. Don’t wait for it

If some task takes time to give you an output that is needed to carry on other tasks, but this first taks do not occupy you, go ahead with other independent tasks. At home, you need to wait for the washing machine to terminate before drying the landry, however you could cook your lunch meanwhile.

This principle invoke two concepts, pipelining and asynchronous.

Piplining is largely used at HW level. The idea is to always occupy resources locally and not to wait for the whole process to finish before starting the next. If you are now drying your landry, your washing machine is free, go ahead with the next batch, don’t wait the drying of the first batch to end…that is the idea.

Asynchronous is the fact that you don’y wait for acknowledgment or return. You will keep cooking your lunch until you hear the biiiiiip biiiiip of your machine. This is the promise that your machine gave you.

Javascript event-loop model (and promises) is a great implementation of asynchronous paradigm.

But what if that task that could be asynchronous requires your support ? That is you need to carry it out. You are no longer “fully” free to do other thing. That bring us to the next section.

3. Do it in parallel

While you are cooking, you could switch from time to time to your sink and wash the vessels. You will jump between the two tasks and you have better to do so. having a lunch ready without a clean vessel to serve it on is hopeless. Same is for having a clean shiny vessel with a carbonized meal.

This is invokes multi-threading and multi-processing. Difference is that the former shares, the context (heap and stack) while the later allocates separate context. It is easier for threads to collaborate and they are lighter to launch while processes are heavier and needs special mechanism to communicates with each others (IPC, shared memory, db, file system...) but processes takes the benefit of dedicated ressources (memory, cpu core, caches…etc) and could be safer, reliable and more performante to carry out heavy tasks.

Both asynchronous and multi-tasking falls under the concurrent programming paradigm. Now you know the difference.

You could go further with the distributed computing systems, where machines collaborate over networks…this is whole discipline.

4. Only process it when you need it

If you need to add an egg to your nice meal, break it at the last moment. Imagine you break it before and put it to wait into a cup. Meanwhile you remember that your friend, which you invited tonight, is allergic to egg. You would have broken the egg for free. In addition, you have to wash the cup…what a pain !

This is the concept of lazyiness. If you can delay a piece of calculation, do it.

It is easy to achieve this by encapsulating the costy bill into a function and return it as a symbol. Definitely, everthing is a symbol.

Dynamically typed languages, like Python or JS, make that easy.

#instead of

a = heavy_calculation

return a #you don’t know when ‘a’ will be used

#better do

a = lambda : heavy_calculation #python

a = function(){ return heavy_calcution} //JS

return a #don’t forget to tell the customer that ‘a’ is now a callable , i.e a()

5. Do it simply

Last and not the least, do things simply. The shortest path is the straight line (not on curved screens though).

Do not try to incorporate sophisticated stuff at the early stage. Make your product fully functional in the most straight forward way. Later then, you could work on improvments.

--

--

Adel Ghamri
0 Followers

Automotive and Computer Science Engineer