lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On Wednesday 19 August 2009 11:48:53 am Gary Bringhurst wrote:
> Hi all,
> 
> I've searched the archives but can't find a post regarding this  
> question.
> 
> I've been timing table accesses under LuaJIT using various key types  
> in a very simple tight loop.  Positive integers are the fastest (of  
> course), while negative integers, tables, and functions used as  
> indices are approximately 33% slower.  That's not bad, given that  
> we're using the hashed storage for the table in these cases.
> 
> But when I test using strings as table keys they time out as over 100%  
> slower, meaning they take twice as long to execute.  I've got the code  
> structured to avoid any string operations, so those aren't a part of  
> the timing.
> 
> This was very surprising to me, because my assumption was that any key  
> would be used in essentially the same way for a lookup.  Does anyone  
> know why string keys are so much slower?  And does this mean that for  
> maximum performance we'd be better off always using "tab[index]"  
> accesses instead of "tab.name" accesses?
> 
> Inquiring minds want to know,  :-)
> -gary
> 
> 

Have you taken into account the time taken to create a new string with the 'local key = "" .. i" statement? I'm not sure, but I thought that creating new strings could be expensive. 

To test indexing performance, you would want to make sure that all of you keys were created outside of the timed loop.

Peter