NCL 604: The Cost of an Operation

In the last three lessons, we've spent a lot of time reducing communication overhead.

We've learned to coalesce instructions, avoid unnecessary line fetches, and keep loops running directly on the CPU.

But there's another important part of performance we haven't considered yet.

How much work does an instruction actually perform?

Not all instructions cost the same amount of time.

Some finish almost immediately. Others can take milliseconds, or even seconds.

And sometimes, the most expensive part of an instruction isn't communicating with another component.

It's the work that component has to do.

Communication and Processing

Let's distinguish two costs we've encountered.

Communication overhead is the time spent sending a request and receiving its response.

Processing time is the time spent performing the requested operation.

For example, consider:

ADD $000 $001 $002

The CPU performs this addition internally. There's no need to communicate with a coprocessor.

Now consider:

S.JOIN $000 "Hello, " "world!"

The CPU sends a request to the string coprocessor and waits for the result.

The operation therefore involves both communication and processing.

These costs are separate, but both contribute to the time an instruction takes.

A simple operation might spend most of its time communicating.

A complicated operation might spend most of its time doing the actual work.

Let's look at some examples.

CPU Operations

The CPU can execute many instructions without involving another component.

For example:

ADD $000 $001 $002

This performs a straightforward integer addition.

Now consider:

SQRT $000 $001

Calculating an integer square root is more complicated than adding two numbers.

Internally, SQRT uses an arithmetic loop to calculate its result.

But that loop runs entirely within the CPU.

Even though SQRT performs considerably more calculations than ADD, both operations are extremely fast.

We're talking about processing times measured in microseconds, rather than the milliseconds we've been using for communication.

This gives us an important distinction:

More calculations don't necessarily mean noticeably slower execution.

If an instruction performs a little extra work locally, that may be insignificant compared with fetching another program line or contacting a coprocessor.

Optimizing away a few arithmetic operations isn't always worthwhile if the program still spends most of its time waiting for communication.

Coprocessor Operations

Coprocessors introduce another layer of work.

We've already seen that calling a coprocessor involves communication.

But the coprocessor also needs time to perform the requested operation.

Consider the string coprocessor.

Simple String Operations

An instruction such as:

S.JOIN $000 "Hello, " "world!"

Joins two strings and returns the result.

The actual string operation is relatively straightforward.

Much of the instruction's total time may be spent communicating with the coprocessor rather than joining the strings.

But not every string operation is so simple.

Iterative String Operations

Consider padding a string to a requested length:

S.PADR $000 "Hello" 20 " "

Or trimming unwanted characters:

S.TRIMR $000 "Hello   "

These operations may need to examine or construct a string over multiple internal processing cycles.

The string coprocessor performs that work using its own scheduling mechanism.

As a result, padding and trimming can take milliseconds to complete, even after the request has reached the coprocessor.

The amount of work also matters.

Padding a string by a few characters isn't necessarily equivalent to padding it by hundreds.

Both operations use a single NCL instruction, but they don't necessarily have the same processing cost.

Instruction count alone doesn't tell us how much work a coprocessor performs.

Peripheral Operations

The difference becomes even more noticeable when we involve peripherals.

A peripheral may need to do substantially more than calculate a value or manipulate a string.

It might need to update a display, transfer stored data, or reorganize an entire file.

And the CPU generally cannot make that work happen any faster.

Display Rendering

Consider:

D.BLT

This is one of the simplest-looking instructions we've encountered.

It has no operands.

But its job is anything but trivial.

D.BLT instructs the display to refresh its entire visible contents.

The display must update its physical tiles, and a complete refresh can take seconds.

That makes D.BLT enormously more expensive than an ordinary arithmetic instruction.

We've already learned in the 300 level that D.CHR is useful for targeted character updates, while D.TXT and D.BLT are used when preparing and displaying larger changes.

Now we can understand the performance implications.

A targeted update avoids the work of refreshing the entire display.

And because D.BLT always performs a full refresh, even a small change to the display buffer doesn't make the refresh itself smaller.

The important distinction is between preparing display content and physically rendering it.

For example:

D.TXT "Hello, world!"
D.BLT

The first instruction prepares the text.

The second tells the display to render its contents.

The rendering can take far longer than preparing the text.

Disk Operations

Disk operations demonstrate a different kind of expensive work.

Consider reading a line from a file:

SYS.DISK.READ $000 "A:/path" 0

The CPU requests the data through the kernel.

The drive communicates with the disk, retrieves the requested information, and eventually returns the result.

From the programmer's perspective, this is a single instruction.

But the work involves an entire storage subsystem.

And some disk operations are considerably more expensive than others.

In particular, inserting or removing lines can require reorganizing existing data.

Imagine a file containing 1,000 lines.

If we insert a new line near the beginning, the disk may need to shift nearly every subsequent line to make room.

Removing a line can require a similar rearrangement to close the gap.

By contrast, modifying data near the end of a file may involve much less work.

The important factor isn't merely the size of the change.

It's how much existing data must be moved to accommodate it.

This is why repeated insertions or removals near the beginning of a large file can become particularly expensive.

The CPU can't eliminate that work through coalescing or hot looping.

It can only avoid requesting unnecessary modifications, or organize its data so that fewer expensive modifications are needed.

One Instruction, Many Costs

Let's bring these examples together.

Operation Main processing work Typical scale
ADD Integer arithmetic Microseconds or less
SQRT Iterative integer calculation Microseconds
S.JOIN String concatenation Relatively inexpensive
S.PAD*, S.TRIM* Iterative string processing Milliseconds
D.BLT Full display refresh Potentially seconds
Disk INSERT, REMOVE Rearranging stored lines Depends on affected data

About Performance Estimates

These timings describe approximate processing costs, not guaranteed instruction execution times.

Actual performance depends on system load, scheduling overhead, and the amount of work being performed.

Operations involving coprocessors and peripherals also incur communication overhead.

Notice how dramatically these operations differ.

A single display refresh might take longer than thousands of CPU arithmetic instructions.

A single disk insertion might require more work than reading many individual lines.

And a complicated CPU instruction might still be effectively instantaneous compared with a simple coprocessor request.

That's why counting instructions isn't enough to estimate a program's performance.

Choosing What to Optimize

The last few lessons have shown us how to reduce the cost of fetching and executing instructions.

But those optimizations don't affect every part of a program equally.

Consider a program that:

  1. Performs several hundred arithmetic instructions.
  2. Prepares a display buffer.
  3. Calls D.BLT.
  4. Waits for the display to finish.

Even if we made the arithmetic calculations ten times faster, the program might barely feel any different.

Why?

Because the display refresh could still account for most of its execution time.

This is an example of a bottleneck: the part of a program that limits its overall performance.

Optimizing something outside the bottleneck may produce very little improvement.

That doesn't make coalescing or hot looping useless.

It means we should understand what our program spends time doing before deciding what to optimize.

Sometimes the answer is to reduce line fetches.

Sometimes it's to avoid unnecessary coprocessor calls.

And sometimes it's to avoid requesting expensive peripheral operations in the first place.

The fastest instruction is often the one we don't need to execute.

What's Next?

We've seen that some operations can take considerably longer than others.

And while we can often reduce unnecessary work, we can't always make an expensive operation finish faster.

A full display refresh still takes time.

But there's another possibility.

What if the CPU could continue doing useful work while the display is busy?

In the next lesson, NCL 605: Non-Blocking Operations, we'll explore how certain instructions allow the CPU and peripherals to work at the same time, and how to take advantage of that without simply moving the wait somewhere else.