Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Getting minutes, seconds and centiseconds from milliseconds
(version: 0)
Comparing performance of:
Math.floor vs using % vs using % v2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
this.ms = Math.floor(Math.random() * 10000000)
Tests:
Math.floor
this.cs = Math.floor(this.ms / 10); this.s = Math.floor(this.cs / 100); this.m = Math.floor(this.s / 60);
using %
this.residue_ms = this.ms % 10; this.residue_cs_in_ms = this.ms % 1000; this.residue_s_in_ms = this.ms % 60000; this.m_in_ms = this.ms - this.residue_s_in_ms; this.s_in_ms = this.residue_s_in_ms - this.residue_cs_in_ms; this.cs_in_ms = this.residue_cs_in_ms - this.residue_ms; this.m = this.m_in_ms / 60000; this.s = this.s_in_ms / 1000; this.cs = this.cs_in_ms / 10;
using % v2
this.residue_ms = this.ms % 10; this.total_cs = (this.ms - this.residue_ms) / 10; this.cs = this.total_cs % 100; this.total_s = (this.total_cs - this.cs) / 100; this.s = this.total_s % 60; this.m = (this.total_s - this.s) / 60;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.floor
using %
using % v2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.floor
12146357.0 Ops/sec
using %
1595322.0 Ops/sec
using % v2
1841941.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition, which is a set of instructions for the test to be executed. In this case, there are three different approaches to extract minutes, seconds, and centiseconds from milliseconds: 1. `this.cs = Math.floor(this.ms / 10);` 2. `using %` (which we'll explore later) 3. `using % v2` **Approach 1: Using Math.floor** The first approach uses the built-in `Math.floor()` function to calculate the centiseconds (`cs`) by dividing `ms` by 10. Pros: * Easy to understand and implement * Built-in function is well-optimized Cons: * May not be as efficient as alternative approaches for large values of `ms` **Approach 2: Using %** The second approach uses a modulo operator (`%`) to calculate the centiseconds, seconds, and minutes. Pros: * Can be more efficient than using `Math.floor()` for large values of `ms` * Avoids the overhead of calling a built-in function Cons: * May require additional calculations to ensure accurate results * Not as straightforward to understand and implement as Approach 1 **Approach 3: Using % v2** The third approach is an optimized version of the second approach, which uses additional modulo operations to reduce the number of calculations. Pros: * Further improves efficiency compared to Approach 2 * Reduces the number of calculations required for accurate results Cons: * More complex and harder to understand than Approaches 1 and 2 * May require more careful attention to avoid errors **Library Used** There is no library explicitly mentioned in the benchmark definition. However, it's likely that MeasureThat.net uses some internal library or framework to execute and optimize the benchmarks. **Special JS Feature/Syntax** None of the approaches use any special JavaScript features or syntax beyond what's considered standard in JavaScript (e.g., `Math.floor()`). **Other Alternatives** Some other alternative approaches might include: * Using bitwise operations (`>>` or `>>>`) to extract minutes, seconds, and centiseconds from milliseconds. * Implementing a custom function using assembly language (if supported by the JavaScript engine). * Using a library like Big.js for arbitrary-precision arithmetic. Keep in mind that these alternatives would likely have different performance characteristics and might not be as well-supported or optimized as the approaches used on MeasureThat.net.
Related benchmarks:
toFixed vs toPrecision vs Math.round() feat. Math.pow
getting seconds from milliseconds
new Date vs custom method
parseInt-vs-math.floor2agasdsgsa
Comments
Confirm delete:
Do you really want to delete benchmark?