π
<-

LUA Progress bar

Pour TI-Nspire OS 3.0 ou ultérieur.

Re: LUA Progress bar

Unread postby Adriweb » 30 Oct 2014, 18:28

FrederikTheisen wrote:I know that the code is not optimized (not checking double will be a nice fix for sure).

See his edit above.
A note about that : in general, memoization (yes that's the right word) is a good idea.
(Edit : well, I mean, rather about "caching" results instead of re-calculating stuff several times - if you can't do it otherwise, of course)

FrederikTheisen wrote:Though many o the constants are declared (as local) outside the code snippet i shared (program is currently 700 lines + 8000 lines database)

Using locals instead of globals whenever you can is better, in short.
Also valid with functions from modules like math etc. : when you use them (especially a lot, like in loops, localize them before, it'll be faster in the loop. See above code with sqrt)

FrederikTheisen wrote:I'm not familiar with the coroutine 'function', but I hope that it will address the problem.

That sure is advanced Lua and will likely be confusing for you if you're only beginning - but don't worry, it's not that scary :)

For more optimization tricks : archives_voir.php?id=6720
(and there is another one more recent but I can't access it because the host (free.fr) recently changed its policy on listing the content of ftp directory... meh)

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
My calculator programs
Mes programmes pour calculatrices
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 80%
 
Posts: 14843
Images: 1133
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
Twitter: adriweb
GitHub: adriweb

Re: LUA Progress bar

Unread postby Jens_K » 30 Oct 2014, 21:47

Glad I could help!
Coroutines are basically threads in lua, altough I don't think real multithreading is possible on the nspire (?). But they are useful when you want to pause and resume functions ;)

Adriweb wrote:(and there is another one more recent but I can't access it because the host (free.fr) recently changed its policy on listing the content of ftp directory... meh)

I found the newer (better) version in my download-folder, so I attached it (I hope that's ok because of copyrights'n'stuff)
You do not have the required permissions to view the files attached to this post.
User avatar
Jens_K
Niveau 0: MI (Membre Inactif)
Niveau 0: MI (Membre Inactif)
Level up: 0%
 
Posts: 7
Joined: 16 Oct 2014, 11:07
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Adriweb » 30 Oct 2014, 21:54

Yep, thanks :)
It's actually "better" for more advanced people - but the one I linked would probably be more understandable to beginners though :) (which was who it was made for originally, too)

I'll make an index.html with links to the other stuff lying in the folder when I have an FTP access to the server.

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
My calculator programs
Mes programmes pour calculatrices
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 80%
 
Posts: 14843
Images: 1133
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
Twitter: adriweb
GitHub: adriweb

Re: LUA Progress bar

Unread postby FrederikTheisen » 31 Oct 2014, 00:48

I was about to post this but it might not be a problem anymore. (yay)
I have got a problem with the way it works, which I'm not able to solve.
The program continues past the coroutine before running the code in the coroutine.

It's probably because I don't understand how it works.

As you can see here I need the maxDist immediatly after I calculate it
Code: Select all
         maxDist = 0
         --getMaxDist()
         CRgetMaxDist = coroutine.create(getMaxDist)
         -- SET MOLECULE SIZE (DISTANCE)
         cDist = 350+maxDist^1.55
      end
   end
   --dataLoad = false
end


I solved the problem by finishing the remaining tasks inside the 'on.timer' event when the progress = 1
Code: Select all
if dataLoadPRG == 1 then
   timer.stop()
   dataLoad = false
   data = true
   cDist = 350+maxDist^1.55
   getRotation()
end


It seems to work ok, but it still takes an awfully long time for large molecules (that is what I'm working with :p ).
User avatar
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Level up: 13.3%
 
Posts: 4
Joined: 30 Oct 2014, 02:01
Gender: Not specified
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby Adriweb » 31 Oct 2014, 00:50

Try without the coroutine (direct call) but do the window invalidate every {x} loop index ? for example, something like if index%100 == 0 then platform.window:invalidate() end

MyCalcs: Help the community's calculator documentations by filling out your calculators info!
MyCalcs: Aidez la communauté à documenter les calculatrices en donnant des infos sur vos calculatrices !
Inspired-Lua.org: All about TI-Nspire Lua programming (tutorials, wiki/docs...)
My calculator programs
Mes programmes pour calculatrices
User avatar
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Level up: 80%
 
Posts: 14843
Images: 1133
Joined: 01 Jun 2007, 00:00
Location: France
Gender: Male
Calculator(s):
MyCalcs profile
Twitter: adriweb
GitHub: adriweb

Re: LUA Progress bar

Unread postby Jens_K » 31 Oct 2014, 01:12

Or do something like this:
Code: Select all
function getMaxDist()
   local sqrt, getMilliSecCounter = math.sqrt, timer.getMilliSecCounter
   local dist, now
   local nAtoms = molTable["nAtoms"]
   for i = 1,nAtoms-1 do
      for j = i+1,nAtoms do
         dist = sqrt((molTable[i]["x"] - molTable[j]["x"])^2  +  (molTable[i]["y"] - molTable[j]["y"])^2  +  (molTable[i]["z"] - molTable[j]["z"])^2)
         if dist > maxDist then
            maxDist = dist
         end
      end
      now=getMilliSecCounter()
      if now>=lastUpdate+updateInterval then
         coroutine.yield(i/(nAtoms-1))
         lastUpdate=now
      end
   end
   coroutine.yield(1)
end

local lastUpdate=timer.getMilliSecCounter()
local updateInterval=1000   --ms
CRgetMaxDist=coroutine.create(getMaxDist)
local progress=0
local finished=false

I don't know how fast the OS gets the system clock's value though, so it might be a bit slower.
User avatar
Jens_K
Niveau 0: MI (Membre Inactif)
Niveau 0: MI (Membre Inactif)
Level up: 0%
 
Posts: 7
Joined: 16 Oct 2014, 11:07
Gender: Male
Calculator(s):
MyCalcs profile

Re: LUA Progress bar

Unread postby FrederikTheisen » 31 Oct 2014, 01:42

Quick test with Jens_Ks proposed change.

The calculator (measured with stopwatch) is about 2 times faster. 50s vs 26s

The PC software (measured using timer.get...) is about 70 times faster. 8444ms vs 122ms

very nice, though I'll never get to see the awesome loading screen on my computer again :/
User avatar
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Level up: 13.3%
 
Posts: 4
Joined: 30 Oct 2014, 02:01
Gender: Not specified
Calculator(s):
MyCalcs profile

Previous

Return to Nspire-Lua

Who is online

Users browsing this forum: ClaudeBot [spider] and 2 guests

-
Search
-
Social TI-Planet
-
Featured topics
Comparaisons des meilleurs prix pour acheter sa calculatrice !
"1 calculatrice pour tous", le programme solidaire de Texas Instruments. Reçois gratuitement et sans aucune obligation d'achat, 5 calculatrices couleur programmables en Python à donner aux élèves les plus nécessiteux de ton lycée. Tu peux recevoir au choix 5 TI-82 Advanced Edition Python ou bien 5 TI-83 Premium CE Edition Python.
Enseignant(e), reçois gratuitement 1 exemplaire de test de la TI-82 Advanced Edition Python. À demander d'ici le 31 décembre 2024.
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
1234
-
Donations / Premium
For more contests, prizes, reviews, helping us pay the server and domains...
Donate
Discover the the advantages of a donor account !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partner and ad
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
2036 utilisateurs:
>2022 invités
>8 membres
>6 robots
Record simultané (sur 6 mois):
29271 utilisateurs (le 11/07/2025)
-
Other interesting websites
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)