Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- namespace nettest
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine($"Rdtsc ticksPerMillisecond: {ticksPerMillisecond}");
- MethodA_UtcNow();
- MethodB_RdtscP();
- MethodC_Rdtsc();
- MethodD_StopwatchGetTimestamp();
- MethodE_EnvironmentTickCount();
- MethodX_SimpleCount();
- }
- private static void MethodA_UtcNow()
- {
- long cnt = 0;
- var doneTime = DateTime.UtcNow.AddSeconds(1).Ticks;
- var startDT = DateTime.Now;
- while (DateTime.UtcNow.Ticks <= doneTime)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("UtcNow Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- private static void MethodE_EnvironmentTickCount()
- {
- long cnt = 0;
- int doneTick = Environment.TickCount + 1000;
- var startDT = DateTime.Now;
- while (Environment.TickCount <= doneTick)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("TickCount Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- static ulong ticksPerMillisecond = CalibrateTSCTicksPerMillisecond();
- private static ulong CalibrateTSCTicksPerMillisecond()
- {
- ulong rate = 0;
- ulong n = 100;
- for (ulong i = 0; i < n; i++)
- {
- ulong res = CountTSCTicksPerMillisecond();
- if (i == 0 || res < rate)
- rate = res;
- }
- return rate;
- }
- private static ulong CountTSCTicksPerMillisecond()
- {
- ulong tsc1 = Rdtsc.TimestampP();
- long ticks2 = DateTime.UtcNow.Ticks + TimeSpan.TicksPerMillisecond;
- while (DateTime.UtcNow.Ticks < ticks2) ;
- ulong tsc2 = Rdtsc.TimestampP();
- return tsc2 - tsc1;
- }
- private static void MethodC_Rdtsc()
- {
- long cnt = 0;
- ulong doneTick = Rdtsc.Timestamp() + ticksPerMillisecond * 1000;
- var startDT = DateTime.Now;
- while (Rdtsc.Timestamp() <= doneTick)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("Rdtsc Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- private static void MethodB_RdtscP()
- {
- long cnt = 0;
- ulong doneTick = Rdtsc.TimestampP() + ticksPerMillisecond * 1000;
- var startDT = DateTime.Now;
- while (Rdtsc.TimestampP() <= doneTick)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("RdtscP Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- private static void MethodD_StopwatchGetTimestamp()
- {
- long cnt = 0;
- long doneTick = Stopwatch.GetTimestamp() + TimeSpan.TicksPerMillisecond * 1000;
- var startDT = DateTime.Now;
- while (Stopwatch.GetTimestamp() <= doneTick)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("Stopwatch Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- private static void MethodX_SimpleCount()
- {
- readyEvent.Reset();
- Thread counter = new Thread(CountBool);
- Thread waiter = new Thread(WaitBool);
- counter.Start();
- waiter.Start();
- waiter.Join();
- counter.Join();
- }
- private static void CountBool()
- {
- long cnt = 0;
- readyEvent.WaitOne();
- var startDT = DateTime.Now;
- while (!done)
- {
- cnt++;
- }
- var endDT = DateTime.Now;
- Console.WriteLine("SimpleCount Time Taken: {0,30} Total Counted: {1,20}", endDT.Subtract(startDT), cnt);
- }
- private static volatile bool done = false;
- private static ManualResetEvent readyEvent = new ManualResetEvent(false);
- private static void WaitBool()
- {
- readyEvent.Set();
- Thread.Sleep(TimeSpan.FromSeconds(1));
- done = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement