How do I create fast and efficient programs? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 3

How do I create fast and efficient programs?

I've checked a lot of programs here on Sololearn challenges. Some of them take very little time compared to others to achieve the same result. What are the things to consider if you want to have a program that gets things done fast and neatly?

20th Sep 2018, 7:58 AM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
4 Réponses
+ 3
The efficiency depends many things. First the language can make a great difference: For most efficiency you should choose C or C++, generally any language that compiles to code that a CPU can read and has little overhead (Assembler can be even more efficient). If you try to get a certain task done you can often choose if you want use less memory or less CPU usage. Depending on the problem there's usually one solution that's more efficient. Also there are problems that can be solved using different algorithms. In this case it matters how well a algorithm scales and how well it works for the specific problem.
20th Sep 2018, 8:41 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 4
Thanks Aaron Eberhardt for the breakdown. I get your point Chris. Rishikesh, thanks for these. I'll apply them to my programs. I did a little digging and I've seen how recursive functions are used when coding. I also found that systems like security systems, where speed matters make use of low level languages like C as Aaron said.
20th Sep 2018, 5:51 PM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
+ 3
If you calculate a value more than once and the result is always the same, only calculate it once instead and save it in a variable. Especially in (nested) loops this can matter a lot. Calculations might look cool and are more memory efficient, but if you care about speed, don't let the CPU work too much.
20th Sep 2018, 9:25 AM
Chris
Chris - avatar
+ 3
1) Stop using unnecessary variables to store values. For eg:- let's assume you are printing sum of two numbers a & b. For that assume you will use another variable c which takes more space than just simply printing a+b. i.e c=a+b; cout<<c; should be programmed as cout<<a+b; And this implies to VERY LONG EQUATIONS TOO. 2) Use dynamic memory allocation whenever you don't know the exact space you will need. This will help to reduce the usage of memory. 3) Use functions and classes wherever needed. 4) Use recursive functions.
20th Sep 2018, 4:09 PM
Rishikesh Jadhav
Rishikesh Jadhav - avatar