yobo89
|
  |
| Joined: 05 Jun 2010 |
| Total Posts: 2341 |
|
|
| 21 Oct 2015 04:35 AM |
How much more efficient is;
for _,v in pairs(table) do print(v) end
than
for k,v in pairs(table) do print(table[k]) end |
|
|
| Report Abuse |
|
|
Datamora
|
  |
| Joined: 06 Jun 2014 |
| Total Posts: 78 |
|
|
| 21 Oct 2015 04:40 AM |
| First one, I guess. It won't call a metatable event as I think. |
|
|
| Report Abuse |
|
|
|
| 21 Oct 2015 04:42 AM |
the 1st one is more efficient as the for i,v in pairs is already browsing the table
the 2nd seems to be indexing the table the 2nd time wich means the amount of work is doubled just to print a property in a table
all of these changes are only in milliseconds though, so you won't catch it by just watching it, you can use tick() to check last time when it went by, the result will always differ but eh.
also, you do realize what you're trying to do is keymapped and regularly inserted elements into a table, right?
for _,v in pairs(table) do print(v) end
-- is for non-keymapped objects, where you can't access values by table[object/v]
for k,v in pairs(table) do print(table[k]) end
-- i don't think this even works but whatever to my logic
|
|
|
| Report Abuse |
|
|
|
| 21 Oct 2015 04:45 AM |
@Data
WTF?
@Yobo
Same thing basically, the seconed one is pointless why would anyone want to do it that way...
Your still getting the value from the table in the same way; why would you want to access the table again by doing; table[k] if you are indexing the table currently.
They might as well just do this:
for i = 1, #Table do print(Table[i]) wait() end |
|
|
| Report Abuse |
|
|
yobo89
|
  |
| Joined: 05 Jun 2010 |
| Total Posts: 2341 |
|
|
| 21 Oct 2015 04:53 AM |
@Data i didn't ask which was more efiicient, i asked how much more efficient.
How much quicker can Lua process. |
|
|
| Report Abuse |
|
|
|
| 21 Oct 2015 05:01 AM |
| The first one most likely, because the second you are indexing through the table then you access it again the retrieve the value. |
|
|
| Report Abuse |
|
|
|
| 21 Oct 2015 05:07 AM |
the time?
idiot
the 2nd one is double the 1st's time |
|
|
| Report Abuse |
|
|
| |
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 21 Oct 2015 05:37 AM |
"you do realize what you're trying to do is keymapped and regularly inserted elements into a table, right?
for _,v in pairs(table) do print(v) end
-- is for non-keymapped objects, where you can't access values by table[object/v]
for k,v in pairs(table) do print(table[k]) end "
All objects in tables have a key. So the both loops in that example would give the same results no matter what the table was filled with.
@OP
The amount of time the second takes is not "double" the time of the first one, it is just the first but with a table lookup each iteration, which would increase the time the loop takes to finish the same as using a global variable in the loop would.
So not very much, it would take a table with a lot of pairs in it to make a real difference. |
|
|
| Report Abuse |
|
|