π
<-
Chat plein-écran
[^]

LUA Progress bar

Pour TI-Nspire OS 3.0 ou ultérieur.

Re: LUA Progress bar

Message non lude Adriweb » 30 Oct 2014, 18:28

FrederikTheisen a écrit: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 a écrit: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 a écrit: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)
Image

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...)
Avatar de l’utilisateur
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 80.2%
 
Messages: 14614
Images: 1218
Inscription: 01 Juin 2007, 00:00
Localisation: France
Genre: Homme
Calculatrice(s):
MyCalcs profile
Twitter/X: adriweb
GitHub: adriweb

Re: LUA Progress bar

Message non lude 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 a écrit:(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)
Fichiers joints
Advanced_TI-Nspire_Lua_Programming.pptx
(2.64 Mio) Téléchargé 106 fois
Avatar de l’utilisateur
Jens_K
Niveau 0: MI (Membre Inactif)
Niveau 0: MI (Membre Inactif)
Prochain niv.: 0%
 
Messages: 7
Inscription: 16 Oct 2014, 11:07
Genre: Homme
Calculatrice(s):
MyCalcs profile

Re: LUA Progress bar

Message non lude 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.
Image

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...)
Avatar de l’utilisateur
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 80.2%
 
Messages: 14614
Images: 1218
Inscription: 01 Juin 2007, 00:00
Localisation: France
Genre: Homme
Calculatrice(s):
MyCalcs profile
Twitter/X: adriweb
GitHub: adriweb

Re: LUA Progress bar

Message non lude 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: Tout sélectionner
         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: Tout sélectionner
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 ).
Avatar de l’utilisateur
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Prochain niv.: 13.3%
 
Messages: 4
Inscription: 30 Oct 2014, 02:01
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile

Re: LUA Progress bar

Message non lude 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
Image

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...)
Avatar de l’utilisateur
AdriwebAdmin
Niveau 16: CC2 (Commandeur des Calculatrices)
Niveau 16: CC2 (Commandeur des Calculatrices)
Prochain niv.: 80.2%
 
Messages: 14614
Images: 1218
Inscription: 01 Juin 2007, 00:00
Localisation: France
Genre: Homme
Calculatrice(s):
MyCalcs profile
Twitter/X: adriweb
GitHub: adriweb

Re: LUA Progress bar

Message non lude Jens_K » 31 Oct 2014, 01:12

Or do something like this:
Code: Tout sélectionner
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.
Avatar de l’utilisateur
Jens_K
Niveau 0: MI (Membre Inactif)
Niveau 0: MI (Membre Inactif)
Prochain niv.: 0%
 
Messages: 7
Inscription: 16 Oct 2014, 11:07
Genre: Homme
Calculatrice(s):
MyCalcs profile

Re: LUA Progress bar

Message non lude 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 :/
Avatar de l’utilisateur
FrederikTheisen
Niveau 2: MI2 (Membre Initié)
Niveau 2: MI2 (Membre Initié)
Prochain niv.: 13.3%
 
Messages: 4
Inscription: 30 Oct 2014, 02:01
Genre: Non spécifié
Calculatrice(s):
MyCalcs profile

Précédente

Retourner vers Nspire-Lua

Qui est en ligne

Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 4 invités

-
Rechercher
-
Social TI-Planet
-
Sujets à la une
Comparaisons des meilleurs prix pour acheter sa calculatrice !
Aidez la communauté à documenter les révisions matérielles en listant vos calculatrices graphiques !
Phi NumWorks jailbreak
123
-
Faire un don / Premium
Pour plus de concours, de lots, de tests, nous aider à payer le serveur et les domaines...
Faire un don
Découvrez les avantages d'un compte donateur !
JoinRejoignez the donors and/or premium!les donateurs et/ou premium !


Partenaires et pub
Notre partenaire Jarrety Calculatrices à acheter chez Calcuso
-
Stats.
1420 utilisateurs:
>1394 invités
>21 membres
>5 robots
Record simultané (sur 6 mois):
6892 utilisateurs (le 07/06/2017)
-
Autres sites intéressants
Texas Instruments Education
Global | France
 (English / Français)
Banque de programmes TI
ticalc.org
 (English)
La communauté TI-82
tout82.free.fr
 (Français)