Wednesday, November 16, 2016

Using std.datetime.StopWatch to time sections of D code

By Vasudev Ram

Accuracy_and_precision image attribution

This D program shows how you can use the std.datetime module from Phobos, D's standard library, to time the execution of sections of D code. I use the StopWatch class from that module. I print the elapsed time in both milliseconds and in microseconds in this program. (Depending on the functions used from this module and the OS and hardware platform, it may also be possible to measure time to an accuracy of hecto-nanoseconds.

The D documentation for the StopWatch class says:

[ StopWatch measures time as precisely as possible.
This class uses a high-performance counter. On Windows systems, it uses QueryPerformanceCounter, and on Posix systems, it uses clock_gettime if available, and gettimeofday otherwise.
But the precision of StopWatch differs from system to system. It is impossible for it to be the same from system to system since the precision of the system clock varies from system to system, and other system-dependent and situation-dependent stuff (such as the overhead of a context switch between threads) can also affect StopWatch's accuracy. ]

Here are a couple of links about the difference between accuracy and precision:

On Wikipedia: Accuracy and Precision

On Accuracy and Precision

The program for timing D code is below, in the file test_stopwatch.d. It is straightforward, so should be somewhat possible for even people unfamiliar with D to understand. It counts from 1 to 1 billion and measures the time taken for that. Note the use of the more human-readable constant 1_000_000_000, using underscores to separate groups of digits, like Perl can. A nice small D feature. Wish more languages had it.
File: test_stopwatch.d
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site:
Product store:

import std.stdio;
import std.datetime;

void foo() {
    // limit is set to 1000 million which is 1 billion.
    auto limit = 1_000_000_000;
    writeln("Counting from 1 to ", limit);
    foreach (i; 1 .. limit) {
        // This is a do-nothing loop since we are only 
        // interested in counting from 1 to limit.

int main(string[] args) {
    // Declare a variable sw of type StopWatch.
    StopWatch sw;
    // Start the stopwatch.
    // Call function foo() which takes up some time 
    // by counting up to some large number.
    // Stop the stopwatch.
    // Get the elapsed time in microseconds.
    auto usecs = sw.peek().usecs;
    // Get the elapsed time in milliseconds.
    auto msecs = sw.peek().msecs;
    // Get the elapsed time in seconds.
    auto seconds = sw.peek().seconds;
    writeln("Time in usecs: ", usecs);
    writeln("Time in msecs: ", msecs);
    writeln("Time in seconds: ", seconds);
    return 0;
Here is the comand to compile the program.
$ dmd test_stopwatch.d
And here is the output of 3 runs:
$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 3136388
Time in msecs: 3136
Time in seconds: 3

$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 4231483
Time in msecs: 4231
Time in seconds: 4

$ test_stopwatch
Counting from 1 to 1000000000
Time in usecs: 4603806
Time in msecs: 4603
Time in seconds: 4

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes

Managed WordPress Hosting by FlyWheel

No comments: