Skip to content

System.Diagnostics.Stopwatch When measuring small time periods #90788

@andreas-synnerdahl

Description

@andreas-synnerdahl

Description

The GetRawElapsedTicks() method dose not handle possebility of negative values.

// Get the elapsed ticks.
private long GetRawElapsedTicks()
{
    long timeElapsed = _elapsed;

    if (_isRunning)
    {
        // If the Stopwatch is running, add elapsed time since
        // the Stopwatch is started last time.
        long currentTimeStamp = GetTimestamp();
        long elapsedUntilNow = currentTimeStamp - _startTimeStamp;
        timeElapsed += elapsedUntilNow;
    }
    return timeElapsed;
}

Simple fix:

private long GetRawElapsedTicks()
{
    long timeElapsed = _elapsed;

    if (_isRunning)
    {
        // If the Stopwatch is running, add elapsed time since
        // the Stopwatch is started last time.
        long currentTimeStamp = GetTimestamp();
        long elapsedUntilNow = currentTimeStamp - _startTimeStamp;

        if (elapsedUntilNow > 0)
        {
            timeElapsed += elapsedUntilNow;
        }
    }
    return timeElapsed;
}

I would like to suggest a little restructuring of the StopMethod on the System.Diagnostics.Stopwatch.

The current implementation allows negativ values in elapsedThisPeriod to be added to _elapsed and resets _elapsed if the total is a negativ value.

public void Stop()
{
    // Calling stop on a stopped Stopwatch is a no-op.
    if (_isRunning)
    {
        long endTimeStamp = GetTimestamp();
        long elapsedThisPeriod = endTimeStamp - _startTimeStamp;
        _elapsed += elapsedThisPeriod;
        _isRunning = false;

        if (_elapsed < 0)
        {
            // When measuring small time periods the Stopwatch.Elapsed*
            // properties can return negative values.  This is due to
            // bugs in the basic input/output system (BIOS) or the hardware
            // abstraction layer (HAL) on machines with variable-speed CPUs
            // (e.g. Intel SpeedStep).

            _elapsed = 0;
        }
    }
}

Would it not make more sens to skip adding negativ values to _elapsed?

        public void Stop()
        {
            // Calling stop on a stopped Stopwatch is a no-op.
            if (_isRunning)
            {
                long endTimeStamp = GetTimestamp();
                long elapsedThisPeriod = endTimeStamp - _startTimeStamp;
                _isRunning = false;

                if (elapsedThisPeriod > 0)
                {
                    // When measuring small time periods the Stopwatch.Elapsed*
                    // properties can return negative values.  This is due to
                    // bugs in the basic input/output system (BIOS) or the hardware
                    // abstraction layer (HAL) on machines with variable-speed CPUs
                    // (e.g. Intel SpeedStep).

                    _elapsed += elapsedThisPeriod;
                }
            }
        }

Or is the rationell due to some performance consideration in regards to the if-statement?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions