[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua Basics: tables as lists?
- From: "Asko Kauppi" <askok@...>
- Date: Tue, 23 Aug 2005 21:41:58 +0300
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