Urgent.News

What's breaking now, across thousands of outlets.

Tech

A faster way to convert a timestamp to Hour, Min, Sec

Many major date/time libraries compute hour, minute and second in a sequential manner, which can be slow. This article explores a faster approach to convert a daily timestamp [0..86399] into Hour, Minute and Second using only two multiplications (and fewer CPU cycles). The speed boost comes from two main ideas: bitwise hacks and manual implementation of division by multiplication and shifting.

Most libraries compute time components sequentially, creating a dependency chain where each step depends on the previous ones. This makes it difficult to parallelize the computation. However, by rearranging the problem and computing intermediate results (like minute) from multiple sources, the dependency chain can be shortened.

One approach is to manually implement a multiplication-shift for division, which can speed up the computation on certain processors. This method is accurate for up to 626 hours and can provide a speed boost on 32-bit and x86 processors, as well as some SIMD chips.

Another approach is to compute intermediate results in reverse order, calculating the second early and working backward through the chain. This method, used in the .NET runtime, performs better than traditional methods due to direct computation of tmin and its availability for concurrent steps. However, some compilers may not recognize this optimization.

In summary, by re-structuring the problem and applying manual optimizations, it is possible to achieve a faster conversion of timestamps to Hour, Minute and Second, with shorter dependency chains and better performance on various processors.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at benjoffe.com →

More in Tech

Acer Predator Atlas 7 Hands On: Size Matters

While the Project DualPlay Mini hybrid console concept may be one of the more intriguing reveals at IFA 2026, it was not the only gaming-related announcement there. Acer also unveiled the Predator Atlas 7, which is a more compact version of the Predator Atlas 8.

More from Monday 7 September →