System.currentTimeMillis vs System.nanoTime

If you’re just looking for extremely precise measurements of elapsed time, use System.nanoTime(). System.currentTimeMillis() will give you the most accurate possible elapsed time in milliseconds since the epoch, but System.nanoTime() gives you a nanosecond-precise time, relative to some arbitrary point. From the Java Documentation: public static long nanoTime() Returns the current value of the most … Read more

What is the best way to repeatedly execute a function every x seconds? [closed]

If your program doesn’t have a event loop already, use the sched module, which implements a general purpose event scheduler. import sched, time s = sched.scheduler(time.time, time.sleep) def do_something(sc): print(“Doing stuff…”) # do your stuff sc.enter(60, 1, do_something, (sc,)) s.enter(60, 1, do_something, (s,)) s.run() If you’re already using an event loop library like asyncio, trio, … Read more

Is DateTime.Now the best way to measure a function’s performance? [closed]

No, it’s not. Use the Stopwatch (in System.Diagnostics) Stopwatch sw = Stopwatch.StartNew(); PerformWork(); sw.Stop(); Console.WriteLine(“Time taken: {0}ms”, sw.Elapsed.TotalMilliseconds); Stopwatch automatically checks for the existence of high-precision timers. It is worth mentioning that DateTime.Now often is quite a bit slower than DateTime.UtcNow due to the work that has to be done with timezones, DST and such. … Read more

System.Timers.Timer vs System.Threading.Timer

This article offers a fairly comprehensive explanation: “Comparing the Timer Classes in the .NET Framework Class Library” – also available as a .chm file The specific difference appears to be that System.Timers.Timer is geared towards multithreaded applications and is therefore thread-safe via its SynchronizationObject property, whereas System.Threading.Timer is ironically not thread-safe out-of-the-box. I don’t believe … Read more

Calculate the execution time of a method

Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET. var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Do not use DateTime to measure time execution in .NET. UPDATE: As pointed out by @series0ne in … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)