Skip to content

Conversation

@stephentoub
Copy link
Member

Fixes #65858

@ghost
Copy link

ghost commented Mar 9, 2022

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Mar 9, 2022

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost assigned stephentoub Mar 9, 2022
@stephentoub stephentoub force-pushed the stopwatchgetelapsedtime branch from 866874e to f118d26 Compare March 9, 2022 03:02
/// <param name="endingTimestamp">The timestamp marking the end of the time period.</param>
/// <returns>A <see cref="TimeSpan"/> for the elapsed time between the starting and ending timestamps.</returns>
public static TimeSpan GetElapsedTime(long startingTimestamp, long endingTimestamp) =>
new TimeSpan((long)((endingTimestamp - startingTimestamp) * s_tickFrequency));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unrelated question about s_tickFrequency
On Unix, we use a constant 1000000000 for frequency calculation, while on Windows we call Kernel32.QueryPerformanceFrequency()). Would it make sense to align them? e.g. making it constant for Windows as well, or implementing frequency shim on Unix; something like:

int64_t SystemNative_GetPerformanceCounterFrequency()
{
    const int64_t defaultFrequency = 1000000000;

    struct timespec spec;
    if (clock_getres(CLOCK_MONOTONIC, &spec) != 0) return defaultFrequency;

    assert(spec.tv_sec < 1); // resolution in seconds is too high to be correct
    assert(spec.tv_nsec > 0);

    return defaultFrequency / spec.tv_nsec;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to call into a native shim. It was changed in #43343.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, Unix always returns something in terms of seconds + nanoseconds. Where-as Windows returns a measurement in unknown units where you have to independently query a frequency to convert it to nanoseconds.

The static APIs on Stopwatch are meant to be minimal overhead and directly return the "raw units" given by the underlying OS functions. This new helper is what allows it to be normalized to TimeSpan units, which are in terms of 100ns ticks.

@stephentoub stephentoub merged commit c9f7f73 into dotnet:main Mar 9, 2022
@stephentoub stephentoub deleted the stopwatchgetelapsedtime branch March 9, 2022 12:42
@ghost ghost locked as resolved and limited conversation to collaborators Apr 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Stopwatch.GetElapsedTime

4 participants