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 » Scripting Helpers
Home Search
 

Re: Which is faster for reading tables?

Previous Thread :: Next Thread 
SCARFACIAL is not online. SCARFACIAL
Joined: 28 Jan 2010
Total Posts: 7970
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
wetcooldued is not online. wetcooldued
Joined: 28 Dec 2010
Total Posts: 51
03 Oct 2011 02:55 AM
I think table 1 is better, But thats just me.
Report Abuse
kingmod123 is not online. kingmod123
Joined: 16 May 2009
Total Posts: 699
03 Oct 2011 03:02 AM
Table 1.

Less writing = better
Report Abuse
wetcooldued is not online. wetcooldued
Joined: 28 Dec 2010
Total Posts: 51
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
wetcooldued is not online. wetcooldued
Joined: 28 Dec 2010
Total Posts: 51
03 Oct 2011 06:04 AM
o.o The master said my method was the best hes a god...
Report Abuse
ClanSmokeJaguar is not online. ClanSmokeJaguar
Joined: 26 Sep 2011
Total Posts: 167
03 Oct 2011 11:34 AM
I love his games.
Report Abuse
Fredfishy is not online. 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 is not online. sdfgw
Top 50 Poster
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
SCARFACIAL is not online. SCARFACIAL
Joined: 28 Jan 2010
Total Posts: 7970
03 Oct 2011 11:55 AM
I thought a generic for loop was slower?

Regards,
~Scarfacial
Report Abuse
Miro034 is not online. 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
VictoriousMaxPower is not online. VictoriousMaxPower
Joined: 14 Jan 2010
Total Posts: 841
03 Oct 2011 12:15 PM
xD
You know what, I think I might you it too!

~/Search It up, Punk!\~
Report Abuse
Fredfishy is not online. 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 is not online. 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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
03 Oct 2011 07:55 PM
for i = 1,#table do *
Report Abuse
swmaniac is not online. 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
ElectricBlaze is not online. ElectricBlaze
Joined: 18 Jul 2011
Total Posts: 22930
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
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
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
Cachinnation is not online. Cachinnation
Joined: 21 Aug 2011
Total Posts: 1797
03 Oct 2011 08:17 PM
What about:

for snob, snoob in next, tab do
print(snoob)
end
Report Abuse
aboy5643a is not online. 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
DevourSoul is not online. DevourSoul
Joined: 22 May 2009
Total Posts: 1
03 Oct 2011 08:32 PM
DevourSoul
Report Abuse
SDuke524 is not online. 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 is not online. Fredfishy
Joined: 21 Mar 2009
Total Posts: 4197
04 Oct 2011 10:46 AM
@Wil, no it wouldn't.
Report Abuse
Wil2 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • 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