i was benchmarking to check how can caching a part position can be better for performance than calling Part.Position
this is the script
script
while task.wait(0.5) do
local loops = 1000
local part = script.Parent
local vecto = Vector3.new(0,0,1)
local start = os.clock()
local pos= part.Position
for i = 1, loops do
part.Position +=vecto
end
local finish = os.clock()
local no_cache_result = (finish - start) / loops
pos= part.Position
task.wait(0.5)
start = os.clock()
for i = 1, loops do
pos +=vecto
part.Position = pos
end
finish = os.clock()
local cache_result = (finish - start) / loops
local difference = no_cache_result-cache_result
if difference<0 then
warn("caching is somehow slower than no caching now")
end
print(difference)
print("perantage : ".. tostring((no_cache_result/cache_result)*100) )
end
I think your problem in your script is you’re not reducing the number of .Position calls in the cashed version. Both versions read .Position once Modify .Position 1000 times and set .Position 1000 times. On top of that your cashed version does extra math with the += operator calling the _add metamethod for vector3. A true cashe would look something like this
local part = script.Parent
local loops = 1000
local vecto = Vector3.new(0, 0, 1)
local pos = part.Position
pos += vecto * loops
part.Position = pos
Here we read .Position once and writes .Position once. Instead of a for loop running 1000 instances we Cache position once, Do math in Lua, and assign once back to the part. I hope I could help
the benchmark was to find what is the performance increase that you get caching when you want to animate a part manually (since tween service loves to eat performance)
sorry for the false solution mark i just saw the reply and said i will read it when i wake up