Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

C# / .NET - get current machine time in microseconds

0 contributions
3 points

C# / .NET it is possible to take machine time in microseconds in following way.

Getting micro time with Stopwatch.GetTimestamp method example

public static class TimeUtils
{
	public static long GetMicroseconds()
	{
		double timestamp = Stopwatch.GetTimestamp();
		double microseconds = 1_000_000.0 * timestamp / Stopwatch.Frequency;

		return (long)microseconds;
	}
}

Example:

long t1 = TimeUtils.GetMicroseconds();

Thread.Sleep(1000);

long t2 = TimeUtils.GetMicroseconds();
long dt = t2 - t1;

Console.WriteLine(t2 + " - " + t1 + " = " + dt + " us");

Output:

196740354043 - 196739353217 = 1000826 us
Notes: 
  • Stopwatch.GetTimestamp returns time from timer mechanism - system up time.
  • The best application of this time could be measuring how much time took some operations.

References

  1. Stopwatch.GetTimestamp Method - Microsoft Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more