-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueStopwatch
API suggestion
#48570
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
I think the That's a personal opinion, but I feel like what is really missing is a static helper method that takes two timestamps and converts them to a In any case, whether it's |
In some scenarios, we may need to record several times continuously on the stack, so I'm wondering that if we could add |
I prefer @kevingosse's suggestion as well, e.g. something like: public class Stopwatch
{
public static TimeSpan GetElapsedTime(long startTimestamp, long endTimestamp) =>
new TimeSpan((endTimestamp - startTimestamp) * s_frequency);
...
} |
I think we should remove the reset. I'd also love to see how the proposed stopwatch API would be used in place of |
I would guess that the proposed |
Yep is still like to see some updated places to see if it's as ergonomic as ValueStopWatch. |
I trust your imagination can extrapolate ;-) Anywhere you store a ValueStopWatch today, you instead just store a long, and anywhere you call GetElapsedTime() today, you instead pass the stored long and Stopwatch.GetTimestamp() to GetElapsedTime(...). If you wanted to save a few characters, you have another overload that's: public class Stopwatch
{
public static TimeSpan GetElapsedTime(long startTimestamp) =>
GetElapsedTime(startTimestamp, Stopwatch.GetTimestamp());
...
} |
I like that overload. I was looking at the places we use it today and it would be a nice drop in replacement to enable passing just the start time. The other thing to consider is small wrapper structs can be convenient target for extension methods if there's any other functionality we'd like to add. Extension methods on long wouldn't be as nice 😁 |
dotnet/roslyn#41205 contains two implementations (one meant to match |
Failure to strongly type the backing value would make the approach no longer a viable replacement for having to implement our own value type replacement for |
Why? |
It's a use-site constraint related to clarity of code. Two different approaches to resolving a problem are presented above, and I'm saying one of them would have more meaningful downstream impact than the other in the context of one specific downstream project. If the constraint we face in dotnet/roslyn matches the constraint other people have when looking at this proposal, it could be an indicator that the proposal wouldn't serve the intended purpose as well as hoped. |
In other words, it's an eye-of-the-beholder style consideration. That's fine. A ton of code uses Frequency directly, and this would clean those up a bit. If someone wants their own struct for style reasons, it's then a trivial helper on top of these also trivial helpers. I don't see a need to expose a struct from the runtime here. |
Background and Motivation
Currently many places are using
ValueStopwatch
instead ofStopwatch
to use reduce allocation, why don't we provide it in the BCL, inspired by https://github.com/dotnet/extensions/blob/master/src/Shared/src/ValueStopwatch/ValueStopwatch.csProposed API
Usage Examples
Possible implement
The text was updated successfully, but these errors were encountered: