Jump to content

pairs vs ipairs


majqq

Recommended Posts

Hey, as the title say i want to know differences between them, and i have also one question related to it.

This way to use loop is much faster than ipairs/pairs, i am right? If yes, then why?

function unglueOnExplosion()
	local attached = getAttachedElements(source)
	for i = 1, #attached do
		if getElementType(attached[i]) == "player" then
			triggerEvent("ungluePlayer", attached[i])
		end
	end
end
addEventHandler("onVehicleExplode", getRootElement(), unglueOnExplosion)

 

And this is slower version of it?

function unglueOnExplosion()
	local attached = getAttachedElements(source)
	for _, v in pairs(attached) do
		if getElementType(v) == "player" then
			triggerEvent("ungluePlayer", v)
		end
	end
end
addEventHandler("onVehicleExplode", getRootElement(), unglueOnExplosion)

 

Link to post
46 minutes ago, Dimos7 said:

Pairs looks is faster than ipairs  

And this one is pairs it is? I mean the way to use loop in following code:

function unglueOnExplosion()
	local attached = getAttachedElements(source)
	for i = 1, #attached do
		if getElementType(attached[i]) == "player" then
			triggerEvent("ungluePlayer", attached[i])
		end
	end
end
addEventHandler("onVehicleExplode", getRootElement(), unglueOnExplosion)

 

So i should use:

for i = 1, #table do

 

Whenever it's possible?

And by the way, can you guys help me with this? I am writing handlings script and i encountered a problem, i don't know how i should apply loop for it.

So, basically i have 2 tables with data, first contains a string for vehicle handling, and second table contains value for this.

 

local valueNames = {
	"mass",
	"turnMass",
	"dragCoeff",
	"centerOfMass",
	"percentSubmerged",
	"tractionMultiplier",
	"tractionLoss",
	"tractionBias",
	"numberOfGears",
	"maxVelocity",
	"engineAcceleration",
	"engineInertia",
	"driveType",
	"engineType",
	"brakeDeceleration",
	"brakeBias",
	"ABS",
	"steeringLock",
	"suspensionForceLevel",
	"suspensionDamping",
	"suspensionHighSpeedDamping",
	"suspensionUpperLimit",
	"suspensionLowerLimit",
	"suspensionFrontRearBias",
	"suspensionAntiDiveMultiplier",
	"seatOffsetDistance",
	"collisionDamageMultiplier",
	"monetary",
	"modelFlags",
	"handlingFlags",
	"headLight",
	"tailLight",
	"animGroup",
}

local carsValue = {
	[434] = 4000, 8000, 1.7, {0.0, 0.0, -0.3}, -- and later more data... 
}

 

Edited by majqq
Link to post
  • 2 weeks later...

When you run a loop in pairs, it grabs not only indexed items but also objects. Objects have keys and values.
for k, v in pairs(table) do
in this case k would be your key, and v would be its value. Keys are always strings.
Say it's not an object, instead its an indexed item, k would become the index, and v would be the value for that index.

ipairs however, loops through ONLY indexed items and it does them in order. So ipairs will not grab objects.
pairs is faster than ipairs since it does not have to sort through the table first.

for i=1, #table do
this is called an Int loop, where it simply goes from 1 number to another, i=start, end
i represents index mostly because people use this loop to grab table index's.
it is by far the fastest loop, as it doesn't have to sort through the table, or return an extra argumented variable.

#table just returns the amount of items in a table

  • Like 2
  • Thanks 1
Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...