How to optimize common things in code?

How to optimize things like for loops, in pairs loops, if conditions and module scripts?

I think the best way, if your bothered about optimisation, would be to look into something called “Time Complexity”. The general premise of time complexity is that you want to reduce the amount of times you have to loop as much as possible such that the “best case scenario” is always achieved. This also means that your worst case scenario - in a linear search this would be the last value - is not that terrible either.

In a more practical basis as well, its reducing the amount of times you have to do powerful operations (such as handling complex maths) - what you should always strive to achieve is the least amount of time for your code to be run in the worst case scenario. Just look at your code and think “is this really the quickest way I can do this, or is their any details i can use to shorten this?”. Excluding searches, which is where you exclude things based on properties, are very useful.

This is referenced on wiki here:

2 Likes

One common mistake is this:

for i,v in pairs(whatever) do
-- code
end

this should be:

for i,v in whatever do
--code
end
1 Like

this doesnt matter much when it comes to optimising
as all next, pairs and ipairs do is iterate through a table and returns an iterator function

generalised for loops do the same thing with not much difference

so i guess its up to preference

1 Like

In my functions, which may differ to yours, I get around 7% faster scripts when removing the pairs.

yea, i using it

char limit

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.