[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua Basics: tables as lists?
- From: William Trenker <wtrenker@...>
- Date: Tue, 23 Aug 2005 14:55:56 -0400
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