Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trig functions
(version: 0)
Which of the built-in trig functions are fastest? slowest?
Comparing performance of:
sin vs cos vs tan vs asin vs acos vs atan vs atan2
Created:
9 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var N = 1000000; var x = new Float32Array(N); var y = new Float32Array(N); var theta = new Float32Array(N); for (var i = 0; i < N; ++i) { x[i] = 100* Math.random(); y[i] = 100* Math.random(); } for (var i = 0; i < N; ++i) { theta[i] = 2 * Math.PI * Math.random(); }
Tests:
sin
var fn = Math.sin; for (var i = 0; i < N; ++i) { fn(theta[i]); }
cos
var fn = Math.cos; for (var i = 0; i < N; ++i) { fn(theta[i]); }
tan
var fn = Math.tan; for (var i = 0; i < N; ++i) { fn(theta[i]); }
asin
var fn = Math.asin; for (var i = 0; i < N; ++i) { fn(x[i]); }
acos
var fn = Math.acos; for (var i = 0; i < N; ++i) { fn(x[i]); }
atan
var fn = Math.atan; for (var i = 0; i < N; ++i) { fn(x[i]); }
atan2
var fn = Math.atan; for (var i = 0; i < N; ++i) { fn(x[i], y[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
sin
cos
tan
asin
acos
atan
atan2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sin
1366.8 Ops/sec
cos
1288.4 Ops/sec
tan
2535.1 Ops/sec
asin
2101.6 Ops/sec
acos
2471.6 Ops/sec
atan
2192.6 Ops/sec
atan2
36.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript trigonometric functions benchmarking. The provided JSON represents a series of individual test cases that measure the performance of different built-in JavaScript trigonometric functions: `sin`, `cos`, `tan`, `asin`, `acos`, and `atan`. The tests are designed to compare the execution speed of these functions for various input values, including angles in radians. Here's what's being tested: * **Math.sin**, Math.cos, and Math.tan**: These functions take a single argument representing an angle in radians. The test generates random angles between 0 and 2π (radians) and measures the time it takes to compute their sine, cosine, or tangent values using these functions. * **Math.asin**, Math.acos, and Math.atan**: These inverse trigonometric functions take a single argument representing a value between -1 and 1. The test generates random input values for these functions and measures the time it takes to compute their results. Now, let's explore the different approaches being compared: * **Single-argument vs. Two-argument `atan`**: The `atan` function with one argument returns the arctangent of a single value, while the version with two arguments returns the arctangent of a pair of values (x and y coordinates). The test compares their performance for both cases. * **Inverse trigonometric functions vs. Trigonometric functions**: This comparison is straightforward, as it measures the speed of computing inverse trigonometric functions versus regular trigonometric functions. Pros and cons of each approach: * Using a single array to store input values (e.g., `theta`) can reduce memory allocation overhead and improve performance. * Reusing the same array for both computation and storage can lead to better cache locality, which might result in faster execution times. * The test case with `atan2` is much slower due to its complexity and the need to perform additional calculations. Other considerations: * **Library usage**: Some tests use libraries like Math.js or MathTron, but they are not explicitly mentioned in the provided JSON. If used, these libraries might affect the results. * **Special JavaScript features or syntax**: The test does not utilize any special JavaScript features or syntax that would require additional explanation. Alternatives to the current benchmarking approach: * Using a different set of input values (e.g., fractional angles) to better represent real-world scenarios. * Incorporating additional tests for other trigonometric functions, such as `atanh` or `acosh`. * Comparing performance with different JavaScript engines or browsers. Keep in mind that this is just an analysis based on the provided information. A deeper understanding of the specific requirements and goals of the benchmarking project would be necessary to provide more tailored advice.
Related benchmarks:
Power vs Square Root functions
Math.pow(x,0.5) vs Math.sqrt(x) vs x**
toFixed vs toPrecision vs Math.round() feat. Math.pow
Math.hypot vs Math.sqrt
Math.pow(x,2) vs Math.sqrt(x)
Comments
Confirm delete:
Do you really want to delete benchmark?