lua-users home
lua-l archive

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


This once again shows that optimizations should always be based on measurements and not intuition. Intuition lies. :)

-----Original Message-----
    From: "William Trenker"<wtrenker@gmail.com>
    Sent: 23.8.05 21:55:56
    To: "Lua list"<lua@bazar2.conectiva.com.br>
    Subject: Re: Lua Basics: tables as lists?
      On 8/22/05, Rici Lake <lua@ricilake.net> wrote:
    > 
    > For loops are not expensive
    > 
    > > myList = {"a", "b", "cde"}
    > > myListString = "|" .. table.concat(myList, "|") .. "|"
    > > a = "cde"
    > > if string.find(myListString, "|"..a.."|") then
    > >     print("found!")
    > > else
    > >     print("not found")
    > > end
    > 
    > That is very expensive
    
    You're right.  I ran some timed tests over large lists and the for
    loop was an order of magnitude faster than concatenating the table
    values into a string and doing a string.find.
    
    My intuition was wrong.  I assumed that a for loop was expensive
    because it meant running the code in that loop through the interpreter
    over and over.  I thought that the single string.find, being done in
    C, would be faster.  But, in fact, the time to run the preparatory
    table.concat -- for large lists -- is comparitivly very slow  and does
    indeed make that approach very expensive.
    
    I'm really beginning to respect the efficiency of the Lua interpreter.
    
    Thanks for the insight,
    Bill