lua-users home
lua-l archive

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


>Version 2 adds "local" for x and i; version 3 adds "local" for myincr too;
>and version 4 uses a "for" instead of a "while":

Please could you explain why local variables are so much faster than
globals.

John

-----Original Message-----
From: Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
To: Multiple recipients of list <lua-l@tecgraf.puc-rio.br>
Date: 26 September 2000 02:28
Subject: Re: Ruby?


>>My naive Lua implementation with the fixes did it in 3.51,
>>adding the local dropped it to 3.00.
>
>I ran the same tests in my machine in 4 versions and got these results:
>
>1 2.52 100% 1.00
>2 1.73 68% 1.45
>3 1.52 60% 1.65
>4 1.05 41% 2.40
>
>Version 1 is the original (fixed) by Bennett Todd:
>
>%  cat 1
>#!/usr/bin/lua
>
>function myincr(n)
>        return n + 1
>end
>
>x = 0
>i = 0
>while i < 1000000 do
>        x = myincr(x)
>        i = i + 1
>end
>
>print(x)
>
>Version 2 adds "local" for x and i; version 3 adds "local" for myincr too;
>and version 4 uses a "for" instead of a "while":
>
>% diff 1 2
>6a7
>> local x,i
>
>% diff 2 3
>2a3,4
>> local myincr
>>
>
>% diff 3 4
>9c9
>< local x,i
>---
>> local x
>11,12c11
>< i = 0
>< while i < 1000000 do
>---
>> for i=1,1000000 do
>14d12
><       i = i + 1
>
>So, simple transformations can get a speedup of 2.4!
>Of course, real scripts spend their time doing useful work and so these
tests,
>like all benchmarks, should be taken as whta they are: simple indications.
>The only real test is your own script.
>--lhf