# vs table.getn()

You see the title, which one is faster? Which one should I use if I wanted to be fast?

1 Like

both table.getn() and #Table are valid as they are both the same exact thing which is to get the length of something, # is just a Lua Operator that allows you to get the number of things (within a table, or the Size of a string) like for example:

-- strings:
local Text = "As an Example"
-- Number of Characters should be "13" if i counted correctly

print(string.len(Text)) -- Longest but is within the string Library
print(Text:len()) -- function Method
print(#Text) -- Simplest, uses the "#" operator to get the number

-- tables:
local Store = {"Apple", "Orange", "Tomato"} -- Food :)

print(table.getn(Store)) -- longest but is within the table Library
print(#Store) -- Simplest, uses the "#" operator to get the number

Which as you can tell, there are plenty of methods to this.
Its just Multiple Methods you can do, but just know that the simplest would be # which is generally the most common one used nowadays.

However table.getn() is faster after testing the time of both.

No, they are not the same. table.getn() is slower and was deprecated in luau. More details in the official documentation.

3 Likes

I tested with this method, and table.getn is faster:

task.spawn(function() -- idk if task.spawn() was necessary
    local t = tick() 
    print(#{"h"}) 
    print(tick() - t) 
end) 

task.spawn(function() 
    local t = tick()
     print(table.getn{"h"}) 
     print(tick() - t) 
end)  -  Studio
  20:24:23.425  1  -  Edit -- #Table
  20:24:23.425  0.00008654594421386719  -  Edit 
  20:24:23.425  1  -  Edit -- table.getn(Table)
  20:24:23.425  0.0000553131103515625  -  Edit 

unless i got the stuff mixed up

Even if it’s faster, it’s still not really recommended that you use it because it’s deprecated:

Edit: I also found a topic in #resources a long time ago about why it is bad to use table.getn. But I don’t remember where it is anymore.

And you also need to increase how many items you add to the table in your benchmarking.

2 Likes

A single iteration isn’t useful for benchmarking. Do it 10,000 times and without the spawn, and see the average result.

2 Likes

I did, It gives me the result of #Table being about 2 times slower than table.getn, but then again, the decimals.

Edit: there was only a few times where table.getn was slower than #Table so ¯\_(ツ)_/¯

Average Result was around 0.0001 for #Table while table.getn was 0.00003

I refer to the official documentation. we cannot dispute a fact.

However, table.getn is slower and provides yet another way to perform an operation, …

2 Likes

I’m not sure where you’re getting that. I couldn’t reproduce it either with the command line or with the profiler.

(each of these numbers is per 100,000 runs of # or getn)

2 Likes

The Command Line,

I:

  • Ran 10,000 times

  • Ran without the spawn

Thats the result I got.
So I’m not sure what I’m doing

You must not be testing correctly because there is no way getn is faster than #. You should also be using os.clock for your benchmarking because tick is slow. Each of my tests show the inverse of what you are saying; # is around 2x faster than getn.

Doing #/getn t when t has 1 entry is not a good way to show each method’s true speed, nor is it useful to only test one iteration (as JarodOfOrbiter said).

local now = os.clock()
local t = table.create(100000, 'a')

for i = 1, 100000 do
	local n = table.getn(t)
end

local _end = os.clock()
print(_end - now, ' - getn')
-----
task.wait(1) -- breathe
-----
local now = os.clock()
local t = table.create(100000, 'a')

for i = 1, 100000 do
	local n = #t
end

local _end = os.clock()
print(_end - now, ' - #')

-- result 1:
-- #: 0.0010630000033415854
-- getn: 0.002520799986086786

-- result 2:
-- #: 0.0008779999916441739
-- getn: 0.0016586000565439463

-- result 3:
-- #: 0.0007659000111743808
-- getn: 0.0016660999972373247
2 Likes

Yeah, that makes more sense now, the data didn’t add up to what they were saying

There is only a one time where getn was lower than # but that is kind of unlikely to happen

If the documentation says it, a person says it, and another person doesn’t, it’s 10000 + 1 vs 1.

Thanks for the info, clearly, # is faster.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.