+ 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?
4 Antworten
+ 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.
+ 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.
+ 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.
+ 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.