|
| 03 Oct 2011 02:38 AM |
local tab = {"Value", "Another value", "Yet another value"}
-- First way of reading a table for i=1, #tab do print(tab[i]) end
-- Another way to do it local i = 1 while tab[i] do print(tab[i]) i = i + 1 end
They each function the same, but which one would be faster?
Regards, ~Scarfacial |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 02:55 AM |
| I think table 1 is better, But thats just me. |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 03:02 AM |
Table 1.
Less writing = better |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 03:03 AM |
Why didnt you use..
local tab = {"Value", "Another value", "Yet another value"} for _,v in pairs(tab) do print(v) end
~I am Bikerking200~
~ I am God! ~ |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 05:53 AM |
wetcooldued's method is the best. There's also the old way as well:
table.foreach(tab,function(v) print(v) end) |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 06:04 AM |
| o.o The master said my method was the best hes a god... |
|
|
| Report Abuse |
|
|
| |
|
Fredfishy
|
  |
| Joined: 21 Mar 2009 |
| Total Posts: 4197 |
|
|
| 03 Oct 2011 11:39 AM |
Yeah, for i, v in pairs is best. Although, the first one would probably be better |
|
|
| Report Abuse |
|
|
sdfgw
|
  |
 |
| Joined: 08 Jan 2009 |
| Total Posts: 41681 |
|
|
| 03 Oct 2011 11:42 AM |
if you have a table of items and want to iterate in a fancy way (e.g. back to front, only halfway in, et cetera) use for i = n
if you don't know the pointers or are just going through all of them routinely, use pairs() |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 11:55 AM |
I thought a generic for loop was slower?
Regards, ~Scarfacial |
|
|
| Report Abuse |
|
|
Miro034
|
  |
| Joined: 07 Oct 2009 |
| Total Posts: 6568 |
|
|
| 03 Oct 2011 12:04 PM |
for i=1, #tab do print(tab[i]) end
Thx for that example, I actually needed a tutorial for tables...
\\..I am Mr.Cool Noob..// \\..Kids these days -_-'..// |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 12:15 PM |
xD You know what, I think I might you it too!
~/Search It up, Punk!\~ |
|
|
| Report Abuse |
|
|
Fredfishy
|
  |
| Joined: 21 Mar 2009 |
| Total Posts: 4197 |
|
|
| 03 Oct 2011 02:02 PM |
for i, v in pairs(table) do
is faster than
for i = 1, #table do |
|
|
| Report Abuse |
|
|
Wil2
|
  |
| Joined: 01 Feb 2008 |
| Total Posts: 728 |
|
|
| 03 Oct 2011 02:55 PM |
| first is better because the second one would just crash the game |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 07:54 PM |
for i,#table do
That method is an INdirect way of accessing a table while there are ways (for i,v in pairs, etc) that access it directly, making the method shown at the top inefficient. |
|
|
| Report Abuse |
|
|
| |
|
swmaniac
|
  |
| Joined: 28 Jun 2008 |
| Total Posts: 15773 |
|
|
| 03 Oct 2011 07:57 PM |
@Wil
Assuming there's a finite number of values in the table, it wouldn't crash. |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 07:59 PM |
Isn't using a numerical value (for i = 1, #table do) faster than using the pairs/ipairs function?
-Like an __AWESOME__ boss |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 08:08 PM |
@ElectricBlaze - The actual execution is faster, however pretty irrelevant due to the fact that the amount of time difference would be way too small to even matter on roblox. The only reason pairs/ipairs is better is for efficiency.
Still, since this is roblox, it really doesn't matter |
|
|
| Report Abuse |
|
|
|
| 03 Oct 2011 08:17 PM |
What about:
for snob, snoob in next, tab do print(snoob) end |
|
|
| Report Abuse |
|
|
aboy5643a
|
  |
| Joined: 20 Nov 2010 |
| Total Posts: 2785 |
|
|
| 03 Oct 2011 08:23 PM |
Don't even bring up ipairs, it's useless. Always use for key, value in pairs(table) do when dealing with iterating through all objects as it won't break if there are no values in the table. You can also directly access the key and value without using ugly workarounds like table[i].
"I like dooly-bops" -- ArceusInator |
|
|
| Report Abuse |
|
|
| |
|
SDuke524
|
  |
| Joined: 29 Jul 2008 |
| Total Posts: 6267 |
|
|
| 03 Oct 2011 08:37 PM |
| Numeric loops are faster than generic loops. Like sdfgw said, if you want it to be faster and you already know all the indexes then use a numeric loop. If you don't know all the indexes then use pairs. |
|
|
| Report Abuse |
|
|
Fredfishy
|
  |
| Joined: 21 Mar 2009 |
| Total Posts: 4197 |
|
| |
|
Wil2
|
  |
| Joined: 01 Feb 2008 |
| Total Posts: 728 |
|
|
| 04 Oct 2011 02:42 PM |
| the second one is a while loop with no wait, and a loop with no wait goes so fast that roblox crashes |
|
|
| Report Abuse |
|
|