Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Initialize Vec2 with int vs float
(version: 1)
Comparing performance of:
Vec2_int vs Vec2_float
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Tests:
Vec2_int
class Vec2_int { x = 0; y = 0; randomize() { this.x = Math.random(); this.y = Math.random(); } length2() { return this.x * this.x + this.y * this.y; } length() { return Math.sqrt(this.length2()); } } let num = 10000 let sum = 0 for (let i=0; i<num; i++) { let v = new Vec2_int(); v.randomize(); sum += v.length(); } sum /= num; console.log(sum);
Vec2_float
class Vec2_float { x = 0.5; y = 0.5; randomize() { this.x = Math.random(); this.y = Math.random(); } length2() { return this.x * this.x + this.y * this.y; } length() { return Math.sqrt(this.length2()); } } let num = 10000 let sum = 0 for (let i=0; i<num; i++) { let v = new Vec2_float(); v.randomize(); sum += v.length(); } sum /= num; console.log(sum);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Vec2_int
Vec2_float
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Vec2_int
294.2 Ops/sec
Vec2_float
288.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined here is focused on comparing the performance of two different implementations of a 2D vector class, `Vec2_int` and `Vec2_float`, which represents mathematical vectors. The two classes differ primarily in how they define their properties. ### Test Cases Overview 1. **Vec2_int**: - **Properties**: Both properties `x` and `y` are initialized to integers (`0`). - **Methods**: - `randomize()`: Assigns random float values to `x` and `y`. - `length2()`: Computes the squared length of the vector. - `length()`: Computes the actual length using the square root of the squared length. - **Usage**: In a loop, 10,000 instances of `Vec2_int` are created, random values assigned, and the lengths are calculated and averaged. 2. **Vec2_float**: - **Properties**: Both properties `x` and `y` are initialized to floats (`0.5`). - **Methods**: The methods are identical to those in `Vec2_int`. - **Usage**: Similar to `Vec2_int`, 10,000 instances are created, random values assigned, and lengths are calculated. ### Comparison of Options - **Initialization with Integers vs. Floats**: - **Vec2_int** starts with integer properties, whereas **Vec2_float** starts with float properties. - **Pros of Using Floats**: The float initialization (`0.5`) might provide an initial value closer to what would realistically be used in vector calculations (e.g., graphics, physics), where floating-point precision is crucial. - **Cons of Using Integers**: The integer initialization may lead to less realistic scenarios if the values will almost always be floating-point due to randomization. ### Performance Results - **Executions per Second**: - `Vec2_float`: 944.72 executions/second. - `Vec2_int`: 913.15 executions/second. - The `Vec2_float` implementation outperformed `Vec2_int` by a small margin. ### Other Considerations - **Memory Usage**: The memory usage might differ slightly for the two classes. Floats typically use more memory than integers (depends on the architecture), which could have a minor impact during garbage collection or memory access during the operation. - **Precision**: Using floats is generally more suited for mathematical calculations, especially in scenarios where precision matters. Integers would lead to poorer performance in real-world vector applications, making `Vec2_float` a more suitable choice for practical applications. - **Garbage Collection**: The JavaScript engine handles object allocation and garbage collection, but the number of instances created can have an effect on performance, especially in high-traffic applications. ### Alternatives - **Typed Arrays**: For numerical computations involving arrays of numbers, such as vectors, using JavaScript's `Float32Array` or `Float64Array` can be more efficient. These typed arrays offer a way to work with binary data and provide better performance for numerical calculations. - **Libraries**: There are various libraries designed for vector math, such as [GlMatrix](http://glmatrix.net/) or [Three.js](https://threejs.org/). These libraries provide optimized and well-tested implementations of vector mathematics that may outperform custom implementations. In summary, while the benchmark is primarily about testing performance under similar mathematical operations, the choice of using integers vs. floats can lead to significant differences in practical applications, with the latter typically providing better performance and accuracy for vector operations.
Related benchmarks:
soa vs aos
soa vs aos
Multiplying lists of vectors - with or without declaration
Multiplying lists of vectors - with or without declaration
Multiplying lists of vectors - with or without declaration
Multiplying lists of vectors - with or without declaration
Multiplying lists of vectors - with or without declaration
function-overload
new instance versus direct assignment
Comments
Confirm delete:
Do you really want to delete benchmark?