Concepts / Best Practices for Time Measurement

Best Practices for Time Measurement

std::chrono is the subnamespace of the C++ standard library that holds the standard facilities for dealing with time-related tasks.

  • Programming

The Problem Every Program Shares

Programs constantly deal with time. They wait for a while, they measure how long something took, they compare two moments, and they schedule work for later. Any of these tasks could be solved by a programmer inventing their own tools from scratch, but C++ does not leave every programmer to do that. The standard library groups its time-handling facilities into one dedicated area.

What do you think happens?

Before reading further, consider this: if the standard library keeps all of its time tools in one dedicated area, what would you need to know in order to find those tools when you see them in code?

Reveal answer
Answer:

You would need to know where that area lives and how to recognize its name in code. This article answers exactly those two questions, and it stops short of the individual time tools themselves, which come in later material.

The article's purpose is to build a map of location and role, so that when you meet a specific time facility later you already know which neighborhood of the standard library it belongs to.

Why the Standard Library Groups Time Tools

The standard library is large, and grouping related facilities together keeps them easy to find. Time-related facilities all serve the same broad purpose, so they are placed in a single area rather than being scattered among unrelated names. When you see a name that points into that area, you immediately know the code is dealing with time.

Think of this article as learning the address of a neighborhood before learning the streets inside it. You do not need to know every street yet. You only need to know where the neighborhood is and what kind of activity happens there.

Reading the Qualified Name std::chrono

The name std::chrono is a qualified name. A qualified name is built from several parts joined together, and reading it from left to right tells you how those parts nest. Each part has a specific job, and the parts combine to point to one entity.

stdstandard library namespace::scope operatorchronosubnamespace inside stdouter namespacesteps into
Which part of std::chrono is the outer namespace, which is the scope operator, and which is the inner name, and how do they combine to refer to one entity?
Formula, step by step
std::chrono
  1. 1
    std is the leftmost part: the standard library's namespace, the outer container.
  2. 2
    :: is the scope operator: it steps from the outer namespace into something inside it.
  3. 3
    chrono is the rightmost part: a subnamespace that sits inside std and holds the time-related area.
Worked example

Reading a qualified name from left to right

Read the qualified name std::chrono and describe what each part contributes to the meaning of the whole name.

  1. 1
    Locate the leftmost part

    The name begins with std, which is the standard library's namespace. This tells you the name belongs to the C++ standard library.

  2. 2
    Find the scope operator

    The double colon :: separates std from what follows. It means the name steps from the outer namespace into something inside it.

  3. 3
    Read the inner name

    The rightmost part, chrono, sits inside std. It is a subnamespace that holds the standard time-related facilities.

  4. 4
    Combine the parts

    Read together, std::chrono is a container within a container: the standard library holds a namespace, and that namespace holds the time-related area.

Result

std::chrono names the time-related subnamespace of the C++ standard library. It is read as nested containers, not as three unrelated words.

Where chrono Sits Inside std

The hierarchy matters because it tells you what contains what. chrono is not a separate top-level namespace standing on its own. It is nested inside std, so every name that lives in chrono is reached through the std outer namespace. The nesting is what allows the name std::chrono to point to one specific area of the library.

stdstandard library namespacestd::chronotime-related subnamespacetime facilitiescovered in later materialcontainsholds
What contains what: chrono is nested inside std, and the time facilities are held within chrono.

Notice what this article does not claim. It establishes the location and role of std::chrono. The individual facilities that live inside it are the subject of later material, so you should not expect this article to teach any specific time tool.

Header Versus Namespace

Knowing the namespace is only half the picture. A namespace name does not by itself give a program access to anything. The program also needs the header that provides the facilities. For the standard time facilities, that header is chrono.

<chrono>a file you includestd::chronoa name you qualify with
How the include line relates to the std::chrono names used in code: the header is a file you include, and the namespace is a scope where names live.

What do you think happens?

A program uses a name that begins with std::chrono. Which of the two, the header or the namespace, is the thing you write at the top of the file with an include line?

  • The namespace std::chrono

  • The header chrono

Reveal answer
Answer:

The header chrono is the thing you include.

The header is a file that provides the facilities, and you bring it in with an include line. The namespace std::chrono is the name you use to qualify the facilities in code. The two share a word, but they play different roles.

Tracing std::chrono in a Small Program

The following fragment is an illustrative example, not a complete program. It shows where the header and the namespace each appear. The include line brings in the standard time facilities, and the qualified name std::chrono is the place those facilities are found once the header is available.

Code walkthrough
#include <chrono>

// std::chrono is the subnamespace of std that holds the time facilities.
// Any time-related name used below will be written as std::chrono followed by a name.
  1. 1
    Line 1 brings the chrono header into the file, so the standard time facilities become available to the program.
  2. 2
    Line 3 explains the namespace, the qualified name whose parts are std and chrono.
  3. 3
    Line 4 describes how later code will refer to time-related names by qualifying them with std::chrono.
  • The include line names the header chrono, the file that provides the standard time facilities.
  • The comment describes the namespace std::chrono, which is where those names live.

Mistakes Beginners Make with std::chrono

  • Common mistake

    Treating the header and the namespace as the same thing

    What it looks like

    Writing std::chrono on an include line, as if it were a file name.

    Why it fails

    The header is a file you include, and the namespace is a scope where names live. They share a word but do different jobs.

    Do this instead

    Write #include <chrono> to bring in the facilities, and write std::chrono when you qualify a name that lives in the namespace.

  • Common mistake

    Reading std::chrono as two unrelated words

    What it looks like

    Thinking std and chrono are independent, so chrono appears to be a top-level name.

    Why it fails

    The qualified name shows nesting. chrono sits inside std, so it is reached through the standard library's namespace.

    Do this instead

    Read the name from left to right as a container within a container.

  • Common mistake

    Expecting this article to teach specific time tools

    What it looks like

    Looking here for the exact facilities that measure elapsed time.

    Why it fails

    This article covers only the location and role of std::chrono. The individual facilities are covered in later material.

    Do this instead

    Use this article as the map, then move to the later material to learn the facilities inside the neighborhood.

Checking Your Reading of a Qualified Name

EASY

A colleague writes the qualified name std::chrono in a code review and says, 'chrono is a separate top-level namespace that the standard library happens to ship.' Explain in your own words whether that statement is accurate, and identify which part of the name shows the relationship you are describing.

Hints
  • Look at the order of the parts in std::chrono, reading from left to right.
  • Think about what the scope operator :: does between std and chrono.

Key Takeaways

  • std::chrono is the subnamespace of the C++ standard library that holds the standard facilities for time-related tasks.
  • A qualified name is read left to right: std is the outer namespace, the double colon :: is the scope operator, and chrono is the subnamespace nested inside std.
  • The chrono header is a file you include, while std::chrono is a namespace you qualify with. The two share a word but are different things.
  • This article establishes where std::chrono lives and what role it plays; the individual time facilities it contains are covered in later material.

More concepts starting with B