How to make a proper server tick?

Insert a time.sleep(0.01) to wait 10 millis between each time poll otherwise your loop polls time continuously without releasing power to the cpu. Edit: That is better, only waits once if needed. Should a huge CPU overload occur, the time to wait could be negative, and in that case 2 actions could be triggered at … Read more

Monotonic clock on OSX

The Mach kernel provides access to system clocks, out of which at least one (SYSTEM_CLOCK) is advertised by the documentation as being monotonically incrementing. #include <mach/clock.h> #include <mach/mach.h> clock_serv_t cclock; mach_timespec_t mts; host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); mach_timespec_t has nanosecond precision. I’m not sure about the accuracy, though. Mac OS X supports three … Read more

Exact time of display: requestAnimationFrame usage and timeline

What you are experiencing is a Chrome bug (and even two). Basically, when the pool of requestAnimationFrame callbacks is empty, they’ll call it directly at the end of the current event loop, without waiting for the actual painting frame as the specs require. To workaround this bug, you can keep an ever-going requestAnimationFrame loop, but … Read more

Best timing method in C?

I think this should work: #include <time.h> clock_t start = clock(), diff; ProcessIntenseFunction(); diff = clock() – start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf(“Time taken %d seconds %d milliseconds”, msec/1000, msec%1000);

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

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

Is setTimeout with no delay the same as executing the function instantly?

It won’t necessarily run right away, neither will explicitly setting the delay to 0. The reason is that setTimeout removes the function from the execution queue and it will only be invoked after JavaScript has finished with the current execution queue. console.log(1); setTimeout(function() {console.log(2)}); console.log(3); console.log(4); console.log(5); //console logs 1,3,4,5,2 for more details see http://javascriptweblog.wordpress.com/2010/06/28/understanding-javascript-timers/

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

Update Aug. 2022: In modern Python 3, import time followed by time.monotonic_ns() might be sufficient. See my new answer to this other question here: High-precision clock in Python. At the time of my answer in 2016 using Python 3.1 on a Raspberry Pi, that didn’t exist. See https://docs.python.org/3/library/time.html#time.monotonic_ns. This is new in Python 3.7. I … 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().