Getting an accurate execution time in C++ (micro seconds)

If you are using c++11 or later you could use std::chrono::high_resolution_clock. A simple use case : auto start = std::chrono::high_resolution_clock::now(); … auto elapsed = std::chrono::high_resolution_clock::now() – start; long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>( elapsed).count(); This solution has the advantage of being portable. Beware that micro-benchmarking is hard. It’s very easy to measure the wrong thing (like … Read more

Multi-threading benchmarking issues

I just recently wrote an answer to a similar question SO: Eigen library with C++11 multithreading. As I’m interested in this topic too and already had working code at hand, I adapted that sample to OP’s task of matrix multiplication: test-multi-threading-matrix.cc: #include <cassert> #include <cstdint> #include <cstdlib> #include <algorithm> #include <chrono> #include <iomanip> #include <iostream> … Read more

How can I get millisecond and microsecond-resolution timestamps in Python?

For Windows: Here’s a fully-functional module for both Linux (works with pre-Python 3.3 too) and Windows: Functions and code samples. Functions include: micros() millis() delay() delayMicroseconds() Python code module (on GitHub as eRCaGuy_PyTime): “”” GS_timing.py -create some low-level Arduino-like millis() (milliseconds) and micros() (microseconds) timing functions for Python By Gabriel Staples http://www.ElectricRCAircraftGuy.com -click “Contact me” … Read more

How to make `setInterval` behave more in sync, or how to use `setTimeout` instead?

You can create a setTimeout loop using recursion: function timeout() { setTimeout(function () { // Do Something Here // Then recall the parent function to // create a recursive loop. timeout(); }, 1000); } The problem with setInterval() and setTimeout() is that there is no guarantee your code will run in the specified time. By … Read more

Windows 7 timing functions – How to use GetSystemTimeAdjustment correctly?

The GetSystemTimeAsFileTimeAPI provides access to the system’s wall clock in file time format. A 64-bit FILETIME structure receives the system time as FILETIME in 100ns units, which have been expired since Jan 1, 1601. The call to GetSystemTimeAsFileTime typically requires 10 ns to 15 ns. In order to investigate the real accuracy of the system … Read more

Hide div after a few seconds

This will hide the div after 1 second (1000 milliseconds). setTimeout(function() { $(‘#mydiv’).fadeOut(‘fast’); }, 1000); // <– time in milliseconds #mydiv{ width: 100px; height: 100px; background: #000; color: #fff; text-align: center; } <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <div id=”mydiv”>myDiv</div> If you just want to hide without fading, use hide().

timeit versus timing decorator

Use wrapping from functools to improve Matt Alcock’s answer. from functools import wraps from time import time def timing(f): @wraps(f) def wrap(*args, **kw): ts = time() result = f(*args, **kw) te = time() print ‘func:%r args:[%r, %r] took: %2.4f sec’ % \ (f.__name__, args, kw, te-ts) return result return wrap In an example: @timing def … Read more

How accurate is Thread.sleep?

Thread.sleep() is inaccurate. How inaccurate depends on the underlying operating system and its timers and schedulers. I’ve experienced that garbage collection going on in parallel can lead to excessive sleep. When doing “real time” simulations you have to correct your main loop for the inaccuracies of sleep. This can be done by calculating the expected … Read more

Stopwatch vs. using System.DateTime.Now for timing events [duplicate]

As per MSDN: The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency … Read more