NCL 601: The Cost of Code
So far, we've concentrated on writing programs that work.
But two programs can produce exactly the same result while taking very different amounts of time to execute.
To understand why, we need to look at how the NCS/e works.
A Computer of Many Parts
The NCS/e isn't a single processor doing everything itself.
The CPU executes instructions, the kernel manages program execution, coprocessors provide additional operations, and peripherals handle devices such as the display and disk drives.
These components communicate through the system's messaging machinery.
When the CPU needs something from another component, it sends a request. The receiving component handles that request and sends a response.
That exchange takes time.
And on the NCS/e, communication is relatively expensive.
Local Work and Communication
Consider a simple arithmetic instruction:
ADD $010 $011 1
The CPU can perform this operation itself.
It reads the operands, calculates the result, and stores it.
Now consider:
D.TXT "Hello!"
The CPU doesn't handle display text itself.
Instead, it sends a request to the display peripheral, which processes the command and acknowledges it.
The CPU waits for that acknowledgement before continuing.
The important difference is that the first instruction executes locally, while the second requires communication with another component.
The Cost of a Round Trip
Communication between components isn't instantaneous.
A request must be dispatched, received, processed, and acknowledged.
Even when the requested operation is simple, the communication itself introduces overhead.
On the NCS/e, a communication round trip can take tens of milliseconds.
For illustration, let's assume each round trip takes 10 ms.
| Round trips | Time spent communicating |
|---|---|
| 1 | 10 ms |
| 10 | 100 ms |
| 100 | 1 second |
| 1,000 | 10 seconds |
These aren't fixed timings. They're an illustration of how communication overhead accumulates.
A single request might seem insignificant, but repeated requests can quickly add up.
Where Communication Happens
Several familiar NCL operations involve communication between components.
| Operation | From | To |
|---|---|---|
| Fetching a program line | CPU | Kernel |
| Making a system call | CPU | Kernel |
| Calling a coprocessor | CPU | Coprocessor |
| Calling a peripheral | CPU | Peripheral |
These operations serve very different purposes, but their communication overhead is broadly comparable.
Line Fetches
The CPU obtains program lines from the kernel.
Before it can execute a line, it must fetch it.
For example:
MOVE $010 0
ADD $010 $010 1
ADD $010 $010 1
ADD $010 $010 1
Each source line requires a separate fetch.
At our illustrative 10 ms per round trip, these four lines account for approximately 40 ms of communication.
That's before considering the time needed to execute their instructions.
System Calls
System calls request services provided by the kernel.
The CPU sends the request, the system handles it, and the CPU receives a response.
As with line fetching, the exchange introduces communication overhead.
Coprocessor Calls
Some NCL instructions are implemented by coprocessors rather than directly by the CPU.
For example:
S.JOIN $010 "Hello, " "world!"
The CPU sends the operation to the string coprocessor and receives the result.
Even a simple coprocessor operation requires communication.
Peripheral Calls
Peripherals follow a similar pattern.
A command such as:
D.TXT "Hello!"
is sent to the display peripheral.
The display processes the request and responds.
Other peripherals, such as disk drives and modems, communicate with the CPU in much the same way.
Communication Isn't the Whole Cost
So far, we've considered only the overhead of sending requests and receiving responses.
The requested operation also takes time to perform.
Some operations require very little work. Others require considerably more.
We'll examine those differences later.
For now, the important point is that communication has a cost of its own, regardless of what the receiving component is asked to do.
What's Next?
We've seen that even simple programs can spend a surprising amount of time fetching their instructions.
But does every instruction really need its own source line?
In NCL 602: Coalescing Instructions, we'll explore what happens when multiple instructions share a line.