[SOLVED]Tricks to writing efficient programs

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Hello forum

I want to understand some terms related to programming

What does it mean to write efficient and fast code?

How do you write fast and efficient code?

Can anyone point me to some tips on writing efficient code

Thanks in advance
Pushkar
 

Papabravo

Joined Feb 24, 2006
21,157
Writing fast or efficient code means using the minimum number of instructions required to perform a task. Each instruction is assigned a fixed number of clock cycles to perform its function. You have to know the instruction set of a machine intimately in order to do this. At a slightly higher level, when using a programming language lice C, you need to know the internals of how the compiler generates machine instructions in order to evaluate alternative coding strategies.

A long time ago this was a vital exercise at a time when machine time cost $1000.00/hour and programmers worked for $2.25 per hour. Now that you can buy a more powerful computer than the ones we programmed in 1962 for $1000.00 total, and a fair to middling programmer makes $75.00/hour (150,000.00/yr) we are content to let the cheap machine waste it's time so you don't have to anymore.
 

Ya’akov

Joined Jan 27, 2019
9,068
Writing fast or efficient code means using the minimum number of instructions required to perform a task. Each instruction is assigned a fixed number of clock cycles to perform its function. You have to know the instruction set of a machine intimately in order to do this. At a slightly higher level, when using a programming language lice C, you need to know the internals of how the compiler generates machine instructions in order to evaluate alternative coding strategies.

A long time ago this was a vital exercise at a time when machine time cost $1000.00/hour and programmers worked for $2.25 per hour. Now that you can buy a more powerful computer than the ones we programmed in 1962 for $1000.00 total, and a fair to middling programmer makes $75.00/hour (150,000.00/yr) we are content to let the cheap machine waste it's time so you don't have to anymore.
As you point out, things have evolved. While people writing kernel code might still try to bum instructions, "efficient code" now has to do with reuse and maintenance. Code that is easy to read, well commented, and properly modularized is "efficient" in the current environment.
 

Papabravo

Joined Feb 24, 2006
21,157
As you point out, things have evolved. While people writing kernel code might still try to bum instructions, "efficient code" now has to do with reuse and maintenance. Code that is easy to read, well commented, and properly modularized is "efficient" in the current environment.
Excellent point, which I alluded to in mentioning the cost of a programmer. There is no worse feeling among management types when a good programmer refuses to touch a piece of legacy software out of fear for what he might find. OTOH there are programmers who will milk that situation for all it is worth. I have done both.
 

Papabravo

Joined Feb 24, 2006
21,157
The next thing to be aware of is to familiarize yourself with various generic algorithms. For example, off the top of your head do you know the fastest way to sort a list of more than 25 numbers? There is an interplay between setup time and execution time that makes the choice non-obvious.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,701
As already mentioned, there is a trade off to be made between efficiency of the program vs efficiency of the programmer.
When considering the efficiency of the program, one has to look at the most inner loop of highly iterative processes. This is where the CPU will spend most of its time.

The inner loop of computer graphics programs is where a lot of time is spent, e.g. draw a line from (x1, y1) to (x2, y2).
The primitive graphics functions on the original Apple Macintosh computer was created by Bill Atkinson, called QuickDraw. This was derived from an earlier version called LisaGraf on the Lisa computer. Bill wrote the functions in Pascal. Then, in order to squeeze the maximum efficiency of the program, the most fundamental core was hand written in 68000 assembler.
 

cmartinez

Joined Jan 17, 2007
8,218
As already mentioned, there is a trade off to be made between efficiency of the program vs efficiency of the programmer.
When considering the efficiency of the program, one has to look at the most inner loop of highly iterative processes. This is where the CPU will spend most of its time.

The inner loop of computer graphics programs is where a lot of time is spent, e.g. draw a line from (x1, y1) to (x2, y2).
The primitive graphics functions on the original Apple Macintosh computer was created by Bill Atkinson, called QuickDraw. This was derived from an earlier version called LisaGraf on the Lisa computer. Bill wrote the functions in Pascal. Then, in order to squeeze the maximum efficiency of the program, the most fundamental core was hand written in 68000 assembler.
And if I remember correctly, the method used to draw vector lines was based on Bresenham's algorithm... which is computationally the most efficient way to draw a line to this day.
 

Papabravo

Joined Feb 24, 2006
21,157
And if I remember correctly, the method used to draw vector lines was based on Bresenham's algorithm... which is computationally the most efficient way to draw a line to this day.
I worked with a Calcomp plotter when I was an undergraduate and working in the time-sharing business. I'm familiar with the algorithm, but not with the name or the origin story. Thanks very much for that vignette.
 

cmartinez

Joined Jan 17, 2007
8,218
I worked with a Calcomp plotter when I was an undergraduate and working in the time-sharing business. I'm familiar with the algorithm, but not with the name or the origin story. Thanks very much for that vignette.
Here's another fun fact: Mr Bresenham also designed the most efficient algorithm to draw circles and arcs.
 

BobTPH

Joined Jun 5, 2013
8,804
Funny, I was going to mention Bresnehan’s algorithm as an example of making code efficient.

I wrote a graphics package for PDP-11 and it used that algorithm, but “compiled” the code for a line into memory so that the increments and tests were constants in the complied code to save a few cycles.

Bob
 

djsfantasi

Joined Apr 11, 2010
9,156
Totally efficient coding techniques do NOT exist.

It depends on the processor, the coding language, the dependence on libraries and the skill of the programmer and his/her familiarity with coding techniques.

Starting with the last, I’d search for code templates.

I’d also depend on the recommendations of the programming languages.

Plus, I’d be aware of the trade off between efficiency and maintenance.

What I’m trying to say, is that there is no one answer to your question. It all depends on several other “dimensions”. And as a programmer, it is your responsibility to be skilled in all dimensions before even attempting to write “efficient@ code.
 

Ya’akov

Joined Jan 27, 2019
9,068
A little clarification, since there are some assumptions in my response that might not be obvious.

In the past, “efficient“ code would have simply meant code that uses the least memory, which was a precious resource, and “fast“ code would have meant something that benchmarks faster and/or something that uses fewer CPU cycles.

As @MrChips pointed out, even today there is a meaning to “fast and efficient” that is like this. When analyzing a program’s requirements, we look for inner loops, the code that does the heavy lifting. It is executed over and over and needs to get done quickly. In that context, we might even offload some of these things to hardware such as FPGAs or ASICs.

So, in that area of programming, “fast and efficient“ demands something different than the general case.

As computer hardware has improved and become cheaper, CPU and memory are no longer the precious and rare resource they once were. In fact a modern programmer with no experience in old systems might have a hard time believing the constraints early programmers had to work within. MCU programmer has a little of that flavor, but even there the numbers look like what large systems used to offer and the CPUs are much more powerful.

There have also been improvements in software with sophisticated operating systems, development environments, and other software tools. This means that some things are in the programmer‘s control in terms of code efficiency and some aren’t but it also means the context of “efficient” for efficient code is now changed.

With the maturation of the toolset and all of it’s moving parts, a good programming shop will have a good code lifecycle mapped out that uses tools to create, maintain, and refactor code as needed. Libraries will be standardized, and cdoing style will be shared to make working on someone else’s program easier.

So, in the context of lifecycle, efficiency means making the lifecycle process more efficient and as I said in my first post, this means, among other things, documentation including commenting your code concerning why not what it does, and subroutine arguments, and data structures, etc. As well as using well designed data structures, variable naming conventions shared with the team (or if you are alone, just meaningful so you can understand your own code after a week away), and properly using a version control system.

So fast: “uses the fewest CPU cycles to accomplish the job” or “does the job within a time that doesn’t cause problems for the rest of the program” or “returns the result to the user in a short enough time they don‘t think something is wrong”.

Efficient: “uses minimal system resources (CPU, RAM, disk) to accomplish the job” or “doesn’t use too much of system resources to cause trouble for other programs” or “supports an optimum the software lifecycle process accounting for the tools and people involved”.

So you can see, it depends on which part of the large and complex ecosystem of computer software you are talking about.
 

fahron

Joined Oct 27, 2016
1
Well,

One of ancient tricks was to put each function implementation into separate C file. It required strong interface implementing skills, but in the end linker used to link only functions (read object code) required.
Another tricky thing was to use C++ double pointers for all complex object passing between modules. It required a little bit mangling with macros to bring pointers to pre-defined state and Purify but whole semantics around object moving was redundant. Either you know how to work with memory or not.
Regardless how efficient code is, still it has to be maintained. So, make some compromise.
 
Top