Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test sin computing
(version: 0)
Comparing performance of:
Math vs cache vs With rotate
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<canvas id="myCanvas" width="400" height="400" style="border:1px solid #000;"></canvas>
Script Preparation code:
canvas = document.getElementById('myCanvas'); context = canvas.getContext('2d'); centerX = canvas.width / 2; centerY = canvas.height / 2; radius = 100; // Adjust this value to change the size of the circle numLines = 60; angleIncrement = (2 * Math.PI) / numLines; cos={} sin={} for (let i = 0; i < 60; i++) { var angle = i * angleIncrement; cos[angle] = Math.cos(angle); sin[angle] = Math.sin(angle); }
Tests:
Math
for (var i = 0; i < numLines; i++) { var angle = i*angleIncrement var x1 = centerX + radius * Math.cos(angle); var y1 = centerY + radius * Math.sin(angle); var x2 = centerX + (radius -10) * Math.cos(angle); var y2 = centerY + (radius -10) * Math.sin(angle); context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); context.stroke(); }
cache
for (var i = 0; i < numLines; i++) { var angle = i*angleIncrement var x1 = centerX + radius * cos[angle]; var y1 = centerY + radius * sin[angle]; var x2 = centerX + (radius -10) * cos[angle]; var y2 = centerY + (radius -10) * sin[angle]; context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); context.stroke(); }
With rotate
for (var i = 0; i < numLines; i++) { var angle = i*angleIncrement // Save the current canvas state context.save(); // Rotate the canvas context.translate(centerX, centerY); context.rotate(angle); // Draw the line from the center to the point on the circle context.beginPath(); context.moveTo(radius, 0); context.lineTo(radius - 10, 0); context.stroke(); // Restore the canvas state for the next iteration context.restore(); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math
cache
With rotate
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of drawing lines on a canvas in different ways. **Options Compared:** * **Math:** This approach directly calculates trigonometric values (sine and cosine) using `Math.cos` and `Math.sin` for each line segment. This relies on the built-in JavaScript math functions. * **cache:** This approach pre-calculates sine and cosine values for specific angles and stores them in an object (`cos` and `sin`). When drawing lines, it retrieves these pre-calculated values instead of recalculating them. This aims to improve performance by avoiding repeated calculations. * **With rotate:** This method uses the canvas's transformation capabilities. It rotates the canvas for each line segment, simplifying the coordinate calculation. **Pros and Cons:** * **Math:** * **Pros:** Simplest implementation. * **Cons:** Potentially slowest as it recalculates trigonometric values for every line segment. * **cache:** * **Pros:** Faster than "Math" because it avoids repeated calculations of sine and cosine. * **Cons:** Requires additional memory to store pre-calculated values. * **With rotate:** * **Pros:** Potentially the fastest as it leverages canvas transformations to simplify geometry. * **Cons:** More complex implementation than the other approaches, as it involves managing canvas state (saving and restoring). **Other Considerations:** * The browser's JavaScript engine optimization plays a significant role in performance. Different engines might handle these methods differently. * The size of the circle (`radius`) influences the number of lines drawn, affecting the overall execution time. A larger radius means more lines and potentially longer execution times. **Alternatives:** * **Using a library like CanvasKit:** Specialized libraries designed for canvas rendering can often provide better performance optimizations than raw JavaScript. * **Vector Graphics Formats (SVG):** For static graphics or those with more complex shapes, SVG might be a more efficient choice as it uses vector representations rather than pixels. Let me know if you have any other questions.
Related benchmarks:
Drawing on Canvas
Canvas roundrect perf
canvas quadraticCurveTo vs art
Canvas roundrect perf comparison check
Comments
Confirm delete:
Do you really want to delete benchmark?