Skip to content
systemcoding.dev
Fundamentals

What a Clock Cycle Is and What Clock Speed Measures

A clock cycle is one tick of the signal that paces a processor. Learn what GHz measures, why it is not performance, and how to convert cycles to time in C.

6 min read
Summarize in AI
Flat illustration of a hex dump with one byte highlighted, with the C, Zig and Rust logos.

Processor advertisements lead with one number: 3.2 GHz, 4.8 GHz. People read it as speed, and it is not quite that. Clock speed counts how often a signal ticks, and what the processor gets done per tick varies widely from one design to the next. To read a datasheet or reason about code timing, you need to know what a tick is, what happens inside one, and why two chips at the same frequency can differ by a large factor.

This post defines the clock cycle, converts between frequency, period and time with a small C17 program, and explains the two quantities, cycles per instruction and instructions per cycle, that turn a clock rate into real performance. It deliberately uses no measured benchmark figures, because those depend on the chip.

What a clock cycle is

A digital circuit is built from storage cells, flip-flops, and the logic between them. A flip-flop captures its input only at a particular moment, and the clock is the signal that defines the moment. It is a square wave that rises and falls at a fixed rate, and on each rising edge, every flip-flop it drives takes in the value presented to it. Between two edges, signals travel through gates and settle to the value the next edge will capture.

One clock cycle is the time between two consecutive rising edges. Its length is the period, and the number of cycles per second is the frequency:

Text
period = 1 / frequency

Frequency is measured in hertz, which means cycles per second: 1 MHz is a million cycles per second, and 1 GHz is a billion. A 1 GHz clock has a period of 1 nanosecond. A 16 MHz microcontroller clock, common on small embedded boards, has a period of 62.5 nanoseconds. A 3 GHz core ticks every 0.333 nanoseconds, and in that time light travels about 10 centimeters, which is a hint at why clock rates are limited by physics.

What limits the clock rate

The clock cannot tick faster than the slowest path between two flip-flops can settle. That path, called the critical path, is a chain of gates whose total delay must be shorter than the period, plus the setup time the next flip-flop needs. If the clock is too fast, a flip-flop captures a value that is still changing, and the result is wrong.

Two things follow. A designer can raise the frequency by cutting long logic paths into shorter stages, which is what a pipeline does: instead of one long operation per cycle, there are several short ones, one per stage. And a higher frequency costs power, because every tick charges and discharges capacitance, and dynamic power grows with frequency and with the square of the supply voltage. That is why frequency stopped climbing in the mid-2000s and designers turned to more cores and wider cores instead.

Converting between cycles and time

The conversions are simple arithmetic, and programs that do timing need them often. This program converts a frequency into a period, then answers a common embedded question: how many cycles does a delay of a given length take?

cycles.cc
#include <stdint.h>
#include <stdio.h>

static double period_ns(double hz) {
    return 1e9 / hz;
}

static uint64_t cycles_for(double hz, double seconds) {
    return (uint64_t)(hz * seconds);
}

int main(void) {
    double clocks[] = { 16e6, 72e6, 1e9, 3e9 };
    const char *names[] = { "16 MHz", "72 MHz", "1 GHz", "3 GHz" };

    for (int i = 0; i < 4; i++) {
        printf("%-7s period = %8.3f ns, 1 ms = %llu cycles\n",
               names[i], period_ns(clocks[i]),
               (unsigned long long)cycles_for(clocks[i], 1e-3));
    }
    return 0;
}
Shell
gcc -std=c17 -Wall -Wextra -O2 cycles.c -o cycles
./cycles
Text
16 MHz  period =   62.500 ns, 1 ms = 16000 cycles
72 MHz  period =   13.889 ns, 1 ms = 72000 cycles
1 GHz   period =    1.000 ns, 1 ms = 1000000 cycles
3 GHz   period =    0.333 ns, 1 ms = 3000000 cycles

On a microcontroller, this arithmetic decides whether a bit-banged protocol is possible. If a signal needs a pulse every 1 microsecond and your clock is 16 MHz, you have 16 cycles to do everything, including the loop overhead. On a desktop core the same 1 microsecond is thousands of cycles, and the interesting question is what the operating system does with them.

Clock speed is not performance

The work a processor completes per second is the product of three things: how fast the clock ticks, how many instructions finish per tick, and how many instructions the program needs. The classic performance equation writes it as time = instructions × cycles per instruction × period.

  • Instructions depends on the program and the instruction set. One architecture may need three instructions where another needs one.

  • Cycles per instruction (CPI), or its inverse instructions per cycle (IPC), depends on the microarchitecture: how wide, how deep the pipeline is, how well it predicts branches, how often it waits for memory.

  • Period is the clock.

Two processors at 3 GHz can differ by a large factor in IPC. An in-order core that finishes at most one instruction per cycle and a wide out-of-order core that can finish several in the same cycle run at the same frequency and deliver different throughput. A chip with a lower clock and a higher IPC can be faster overall. This is why comparing GHz across different architectures says little, and comparing within one product family says more.

Memory makes the point sharper. When a load misses the cache and goes to RAM, the core may wait for hundreds of cycles. Raising the clock does not shorten the wait measured in nanoseconds, because DRAM does not speed up with the core, so a program bound by memory gets little from a faster clock.

Cycles in practice: reading the time stamp counter

x86 processors expose a counter that increases with time, the time stamp counter, read by the rdtsc instruction through the __rdtsc intrinsic in <x86intrin.h>. On processors with an invariant TSC, which includes all current x86-64 parts, it ticks at a constant rate that does not change when the core speeds up or slows down, so it counts reference cycles and not actual core cycles.

tsc.cc
#include <stdio.h>
#include <x86intrin.h>

int main(void) {
    unsigned long long start = __rdtsc();
    volatile unsigned long long sink = 0;
    for (int i = 0; i < 1000; i++)
        sink += i;
    unsigned long long end = __rdtsc();

    printf("elapsed reference cycles: %llu\n", end - start);
    return 0;
}
Text
elapsed reference cycles: 1944

The number printed will differ on every run and every machine. It depends on the TSC rate, on whether the code and data were in cache, on interrupts, and on out-of-order reordering of rdtsc itself, which is not a serializing instruction. Use it to compare two pieces of code under identical conditions, with several repetitions, and not to compute an absolute cost of one instruction. For a wall-clock time, clock_gettime(CLOCK_MONOTONIC, &ts) from <time.h> is portable and honest about its resolution.

Where the clock model breaks down

The single clock is a simplification. Real chips run different parts at different rates: the core, the memory controller and each bus have their own clocks, derived from a reference by phase-locked loops. Many designs are partly or fully asynchronous. Modern CPUs also change frequency constantly through dynamic frequency scaling, so "3 GHz" is a base or boost figure, not a constant. Microcontrollers often switch clock sources, from a slow internal oscillator at start-up to a fast PLL-derived clock after the boot code configures it, and timing code written for the wrong one is a classic bug.

Also inHardware
Taggedcbeginner
Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.