Giter VIP home page Giter VIP logo

Comments (3)

TomDotBat avatar TomDotBat commented on September 4, 2024 1

The benchmark below shows a very minimal improvement, however the execution time of the square root calculation ultimately depends on the input given.

local benchmark
local vec = VectorRand() * 1000
local getLength = vec.Length
local getLengthSqr = vec.LengthSqr

print("Benchmark data : ", vec, "\n")

do
    benchmark = SysTime()

    for i = 1, 2e5 do end

    print("Baseline : ", SysTime() - benchmark)
end


do
    benchmark = SysTime()

    for i = 1, 2e5 do
        getLength(vec)
    end

    print("Vector:Length() : ", SysTime() - benchmark)
end


do
    benchmark = SysTime()

    for i = 1, 2e5 do
        getLengthSqr(vec)
    end

    print("Vector:LengthSqr() : ", SysTime() - benchmark)
end

print("\n")

Results (after 5 runs):

Benchmark data : 	495.429901 739.004211 436.868652	

Baseline : 	9.6400000074937e-05
Vector:Length() : 	0.0059317999998711
Vector:LengthSqr() : 	0.0055161000000226


Benchmark data : 	-522.012939 -765.965698 -75.552155	

Baseline : 	8.6899999814705e-05
Vector:Length() : 	0.0058865999999398
Vector:LengthSqr() : 	0.0055757000000085


Benchmark data : 	256.878876 818.915649 -309.296326	

Baseline : 	0.0001259000000573
Vector:Length() : 	0.0059771999999612
Vector:LengthSqr() : 	0.0057152000001679


Benchmark data : 	375.659027 -366.963501 -569.133728	

Baseline : 	9.0699999873323e-05
Vector:Length() : 	0.0060378999999102
Vector:LengthSqr() : 	0.0056388000000425


Benchmark data : 	-381.458496 -671.425659 715.860413	

Baseline : 	8.87999999577e-05
Vector:Length() : 	0.0059182999998484
Vector:LengthSqr() : 	0.0056335000001582

from imgui.

wyozi avatar wyozi commented on September 4, 2024

Can you (or anyone) provide a benchmark proving that it would be more efficient?

Squared distance being so much more efficient than normal distance that it should be changed everywhere is probably the biggest misunderstanding around gmod devs. I wouldn't necessarily be against changing it here if done properly, since it wouldn't affect code complexity that much, but oh boy does everyone like suggesting this "optimization" without actually testing if it has any impact first.

from imgui.

wyozi avatar wyozi commented on September 4, 2024

Thanks! So ~5% improvement (or very roughly 10000th of a millisecond per call).

I wouldn't mind the change here, since it's very localized and requires only two lines changed. It is quite a micro-optimization, however. I guess crossing the C++ boundary is the source of slowness here.

from imgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.