Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,29 @@ The Code: [files/localplayer_cached_or.lua](files/localplayer_cached_or.lua)



# Angle:Zero() vs new Angle()
# Angle:Zero() vs new Angle() vs Cached Angle

TL;DR: It is a faster to reuse the same Angle object and zero it before use than to create a new Angle object everytime.
TL;DR:
Reusing an existing Angle object is significantly faster than creating a new one every time. The fastest approach, however, is returning a pre-initialized cached Angle(0,0,0).

The wiki states this as fact, and it is true. It saves you the overhead of creating a new object and garbage-collecting the old one.
Check the code for more info.
The benchmark confirms what we would expect from a performance standpoint. Creating a new Angle object introduces allocation overhead and later garbage collection cost.
Calling ang:Zero() is already much faster because it reuses the same object and simply resets its values instead of allocating a new one.
The most efficient approach in this test is returning a fully cached Angle(0,0,0) without calling any method or creating a new instance.

Result:

--- Benchmark complete
On Server
reps 30 rounds 5000
Zero'd 1.3485066712595e-07
Create 2.3365133318293e-07
--- Benchmark complete
On Client
reps 30 rounds 5000
Zero'd 1.1277866673481e-07
Create 2.4979333345679e-07
reps 30 rounds 5000
On Server
Zero'd 1.0690399993981e-07
Cached 5.7774666679128e-08
Create 3.0156200007203e-07
--- Benchmark complete
reps 30 rounds 5000
On Client
Zero'd 1.0355866663569e-07
Cached 5.5381333346152e-08
Create 1.8186866671461e-07

The Code: [files/angle_zero_vs_new.lua](files/angle_zero_vs_new.lua)

Expand Down
27 changes: 16 additions & 11 deletions files/angle_zero_vs_new.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
--[[
--- Benchmark complete
On Server
reps 30 rounds 5000
Zero'd 1.3485066712595e-07
Create 2.3365133318293e-07
--- Benchmark complete
On Client
reps 30 rounds 5000
Zero'd 1.1277866673481e-07
Create 2.4979333345679e-07
--- Benchmark complete
reps 30 rounds 5000
On Server
Zero'd 1.0690399993981e-07
Cached 5.7774666679128e-08
Create 3.0156200007203e-07
--- Benchmark complete
reps 30 rounds 5000
On Client
Zero'd 1.0355866663569e-07
Cached 5.5381333346152e-08
Create 1.8186866671461e-07
--]]

local ZeroCached =Angle(0,0,0)
local ang = Angle(0,0,0)
LuctusCompareOften(30,0.1,5000,{
{"Zero'd",function()
ang:Zero()
return ang
end},
{"Cached",function()
return ZeroAng
end},
{"Create",function()
return Angle(0,0,0)
Expand Down