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
|
  |
| 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 |
|
|
| |
|
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 |
|
|
|
| 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 |
|
|
|
| 25 Feb 2012 11:05 PM |
@Flurite
neat beans, I usually use pairs, now I can use ipairs when i need it :) |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
Davidii
|
  |
| Joined: 17 Jul 2008 |
| Total Posts: 1282 |
|
|
| 26 Feb 2012 08:19 AM |
| Oysi, one word: DAAAAAAAAAAAAAAAAAAAAAAAAAAANG. |
|
|
| Report Abuse |
|
|
1Ra
|
  |
| Joined: 02 May 2010 |
| Total Posts: 2400 |
|
|
| 26 Feb 2012 08:27 AM |
| I dont think that is a word... |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
|
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
|
| 26 Feb 2012 10:42 AM |
| When I said "my thing", I meant "what I do". |
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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
|
  |
| 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
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
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
|
  |
| 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 |
|
|