generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Discussion: pairs() or ipairs()?

Previous Thread :: Next Thread 
Davidii is not online. Davidii
Joined: 17 Jul 2008
Total Posts: 1282
25 Feb 2012 10:03 PM
I prefer pairs() because it can iterate negative table indexes. Thoughts?
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
25 Feb 2012 10:08 PM
pairs() is better.

I've heard that pairs() is faster than ipairs() in both numerical and not-numerical indexes.

~Flurite
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
25 Feb 2012 10:13 PM
What is the difference?
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
25 Feb 2012 10:38 PM
xXxMoNkEyMaNxXx,

1. Get an easier name to type.
2. ipairs() only handles numerical indexes and will skip over non-numerical indexes.

~Flurite
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
25 Feb 2012 10:46 PM
I always use pairs. The only cases where you should use ipairs is when you want to ignore non-numerical indices.

@Flurite

He was previously known as Krakow10, while banned. I miss these times. I could type his name easily. But, now... :/
Report Abuse
xXxMoNkEyMaNxXx is not online. xXxMoNkEyMaNxXx
Joined: 03 Oct 2008
Total Posts: 3120
25 Feb 2012 11:05 PM
@Flurite

neat beans, I usually use pairs, now I can use ipairs when i need it :)
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
25 Feb 2012 11:19 PM
I don't use either... I do it the pirate's way:

for i, v in next, table do
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 07:27 AM
[ Content Deleted ]
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 07:33 AM
[ Content Deleted ]
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 07:37 AM
[ Content Deleted ]
Report Abuse
Davidii is not online. Davidii
Joined: 17 Jul 2008
Total Posts: 1282
26 Feb 2012 08:19 AM
Oysi, one word: DAAAAAAAAAAAAAAAAAAAAAAAAAAANG.
Report Abuse
1Ra is not online. 1Ra
Joined: 02 May 2010
Total Posts: 2400
26 Feb 2012 08:27 AM
I dont think that is a word...
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
26 Feb 2012 09:27 AM
Pairs and next are not directly the same thing. What pairs RETURNS is 'next'. It wouldn't be true if you did thsi:


print(pairs == next)
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 09:34 AM
[ Content Deleted ]
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
26 Feb 2012 09:42 AM
I'm using next, not pairs. Yes, pairs returns next, so when you use pairs you're using next, so that means you're actually using my way...
Report Abuse
BlueTaslem is not online. BlueTaslem
Joined: 11 May 2008
Total Posts: 11060
26 Feb 2012 09:52 AM
They are completely distinct in function. You can't simply choose to use one or the other.
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
26 Feb 2012 10:24 AM
I create my own iterator, depending on the task - other than that, I would use pairs() for iterating through the whole table.

I HARDLY use ipairs().
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 10:40 AM
[ Content Deleted ]
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
26 Feb 2012 10:42 AM
When I said "my thing", I meant "what I do".
Report Abuse
Merlin11188 is not online. Merlin11188
Joined: 20 Dec 2007
Total Posts: 4158
26 Feb 2012 12:38 PM
These are my tests. The table I used to test was only numeric indices.
- 3071 ms ; Custom-made iterator
- 1496 ms ; ipairs
- 969 ms ; Numeric For

And that is why I don't use ipairs. Sure, my code looks uglier with a numeric for, but it's pretty obvious what it does.
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
26 Feb 2012 01:41 PM
"- 3071 ms ; Custom-made iterator
- 1496 ms ; ipairs
- 969 ms ; Numeric For"

THAT IS TERRIBLE? How bad is your computer?! Either you created a giant table, or your computer is really bad.

It shouldn't take a second to iterate through a table.
Report Abuse
Quenty is not online. Quenty
Joined: 03 Sep 2009
Total Posts: 9316
26 Feb 2012 02:15 PM
He probably had a big table. However, there IS a limit of the size of the table. ROBLOX actually will error. :P
Report Abuse
Oysi is not online. Oysi
Joined: 06 Jul 2009
Total Posts: 9058
26 Feb 2012 02:21 PM
[ Content Deleted ]
Report Abuse
nightname is not online. nightname
Joined: 10 Jun 2008
Total Posts: 8960
26 Feb 2012 02:31 PM
I got these results:

Custom iterator: 0.187
pairs: 0.078
ipairs: 0.281
numeric for: 0.125
Report Abuse
sncplay42 is not online. sncplay42
Joined: 27 Nov 2008
Total Posts: 11891
26 Feb 2012 03:37 PM
Your subtractions are measuring the wrong time intervals.

local it = 100000
local t = {"a", "b", "c", "d", "e"}
local customIterator = function(t, i) i = (i or 0) + 1
return i <= #t and i or nil, t[i]
end
local pairs = pairs
local ipairs = ipairs

local time0 = os.clock()
for j = 1, it do
for i, v in customIterator, t do
end
end
local time1 = os.clock()

for j = 1, it do
for i, v in pairs(t) do
end
end
local time2 = os.clock()

for j = 1, it do
for i, v in ipairs(t) do
end
end
local time3 = os.clock()

for j = 1, it do
for i = 1, #t do
local v = t[i]
end
end
local time4 = os.clock()

print("Custom iterator:", time1 - time0)
print("pairs:", time2 - time1)
print("ipairs:", time3 - time2)
print("numeric for:", time4 - time3)

Custom iterator: 0.151
pairs: 0.077
ipairs: 0.086
numeric for: 0.037

Numeric for wins.

And besides in most real cases what you do in the loop will effect the times more than the loop itself.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image