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: Efficiency

Previous Thread :: Next Thread 
yobo89 is not online. 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 is not online. 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
AmbientOcclusion is not online. AmbientOcclusion
Joined: 10 Feb 2009
Total Posts: 1323
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
MKofOrbiter is not online. MKofOrbiter
Joined: 28 Aug 2010
Total Posts: 910
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 is not online. 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
MKofOrbiter is not online. MKofOrbiter
Joined: 28 Aug 2010
Total Posts: 910
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
AmbientOcclusion is not online. AmbientOcclusion
Joined: 10 Feb 2009
Total Posts: 1323
21 Oct 2015 05:07 AM
the time?

idiot

the 2nd one is double the 1st's time
Report Abuse
MKofOrbiter is not online. MKofOrbiter
Joined: 28 Aug 2010
Total Posts: 910
21 Oct 2015 05:08 AM
to retrieve the value*
Report Abuse
chimmihc is not online. 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
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