Varying speed of a program.
05-21-2022, 09:46 AM (This post was last modified: 05-21-2022 11:49 AM by matalog.)
Post: #1
 matalog Member Posts: 208 Joined: May 2021
Varying speed of a program.
I have been trying to measure how long a program takes to run using TICK:

Code:
T1:=TICKS CODE T2:=TICKS PRINT(T2-T1).

Which seems to do what it says it will.

The problem is that it is different every time I run the program it returns a different length. There is no random WAIT() or anything that I would have thought would vary the runtime of the program.

Any Ideas what causes the program runtime to change?

To give an idea, it has been varying between 51 and 42 seconds for one program, and between 55 and 50 seconds for another. It seems to run fastest when the calculator has just been turned on.
05-21-2022, 10:02 AM
Post: #2
 albud44 Junior Member Posts: 8 Joined: Jul 2021
RE: Varying speed of a program.
Bonjour,

On va éventuellement confirmer ou infirmer, mais je pense aux possibilités suivantes :

- soit à problème de gestion de mémoire en lien avec les applications ou autre.
- soit à un vidage de la mémoire qui n'intervient pas quand il faudrait ou en tout cas aléatoirement en apparence.

(05-21-2022 09:46 AM)matalog Wrote:  I have been trying to measure how long a program takes to run using TICK:

Code:
T1:=TICK CODE T2:=TICK PRINT(T2-T1).

Which seems to do what it says it will.

The problem is that it is different everytime I run the program. There is no random WAIT() or anything that I would have thought would vary the runtime of the program.

Any Ideas what causes the program runtime to change?

To give an idea, it has been varying between 51 and 42 seconds for one program, and between 55 and 50 seconds for another. It seems to run fastest when the calculator has just been turned on.
05-21-2022, 05:01 PM (This post was last modified: 05-21-2022 11:45 PM by matalog.)
Post: #3
 matalog Member Posts: 208 Joined: May 2021
RE: Varying speed of a program.
Running this program 4 times, I get 4 different runtimes, increasing from time of startup. 187,288, 190,297 and 199,373 200,310.

Code:
EXPORT SPTEST() BEGIN DIMGROB_P(G1,320,240); DIMGROB_P(G2,320,240); LOCAL T1:=TICKS;LOCAL T2:=0;LOCAL T:=-1; RECT();     FOR X FROM 0 TO 5000000 DO      IF X MOD 50000=0 THEN        T:=T+1;        RECT_P(G2,0,0,320,240);        TEXTOUT_P(STRING(T)+"%",G2,0,0,7);        FOR H FROM 0 TO 30 DO           FOR W FROM 0 TO 50 DO              FOR A FROM 0 TO 3 DO                FOR B FROM 0 TO 3 DO                    PIXON_P(G1,80+W*4+B,50+H*4+A,GETPIX_P(G2,W,H));                END;              END;           END;        END;            RECT_P(G0);            BLIT_P(G0,0,0,320,240,G1,0,0,320,240);      END;     END; T2:=TICKS; PRINT(T2-T1); END;

Are you saying that this is a known problem, and someone is looking into it?
05-21-2022, 10:09 PM
Post: #4
 Gene Moderator Posts: 1,290 Joined: Dec 2013
RE: Varying speed of a program.
(05-21-2022 10:02 AM)albud44 Wrote:  Bonjour,

On va éventuellement confirmer ou infirmer, mais je pense aux possibilités suivantes :

- soit à problème de gestion de mémoire en lien avec les applications ou autre.
- soit à un vidage de la mémoire qui n'intervient pas quand il faudrait ou en tout cas aléatoirement en apparence.

Gene as Moderator: English only please in the forums. No one here will mind if the english used is not 100% British or American. I assure you no one wants to see my French or Latin!

----------------------------
Hello,

We will eventually confirm or deny, but I am thinking of the following possibilities:

- either a memory management problem related to applications or other.
- either to a dumping of the memory which does not intervene when it should or in any case randomly in appearance.
05-21-2022, 11:47 PM (This post was last modified: 05-21-2022 11:48 PM by matalog.)
Post: #5
 matalog Member Posts: 208 Joined: May 2021
RE: Varying speed of a program.
I'm not sure if that reply means that it is a known issue.

Can anyone run that program I listed in the second last post a couple of times and see if they also experience the difference in runtimes?

I have not experienced programming languages that have different run-times in the same programs! It may be a useful way to generate random numbers along the way, through sub-programs being run :-).
05-22-2022, 01:34 AM (This post was last modified: 05-22-2022 01:38 AM by anyfoo.)
Post: #6
 anyfoo Junior Member Posts: 38 Joined: May 2020
RE: Varying speed of a program.
(05-21-2022 11:47 PM)matalog Wrote:  I have not experienced programming languages that have different run-times in the same programs!

Oh, but there are plenty which do just by themselves. Anything with a JIT (the more dynamic the worse) or with a garbage collector, unless the start conditions are exactly the same. Even if they are or even if your language is entirely deterministic (say it's compiled C), modern operating systems will switch the core away from the current thread to whatever else needs running, and that is not deterministic. And even if that is not the case, interrupts and varying times to handle them still aren't deterministic and effectively introduce some "jitter". And a modern general purpose CPU will have caches and instruction pipelines that add onto that.

If you wanted to benchmark something on a C64 in the 80s, you already better took the average of multiple runs. Try on anything modern that isn't a simple microcontroller, and you have to make a lot of runs just to "warm up" the system to fill its caches and so on. (There are "real time" operating systems, but those mostly just give upper bound guarantees, not full determinism.)

The question is how significant the differences are, and whether it makes sense given e.g. the system's time slices for threads.

If you change your program to calculate, say, 5 times as much, does the absolute jitter stay the same or does it seem to scale with the amount of calculation it has to perform?
05-22-2022, 09:26 AM
Post: #7
 Terje Vallestad Member Posts: 153 Joined: Dec 2013
RE: Varying speed of a program.
(05-21-2022 11:47 PM)matalog Wrote:  I'm not sure if that reply means that it is a known issue.

Can anyone run that program I listed in the second last post a couple of times and see if they also experience the difference in runtimes?

25,085
-106
-101
-143
-238
-543
-114
-114
-150
-143

All on iPad, the four last it was laying still on a table, the earlier ones I was holding the tablet and moving a bit.
05-23-2022, 10:55 AM
Post: #8
 Kevin Ouellet Member Posts: 149 Joined: Dec 2013
RE: Varying speed of a program.
I noticed that sometimes after running my program Supersonic Ball a few times for a while that the game will slow down occasionally during execution, especially when exiting with the ON key. Rebooting fixes it.

-DJ Omnimaga
https://djomnimaga.music-2000.com
05-24-2022, 08:43 PM
Post: #9
 matalog Member Posts: 208 Joined: May 2021
RE: Varying speed of a program.
(05-22-2022 01:34 AM)anyfoo Wrote:
(05-21-2022 11:47 PM)matalog Wrote:  I have not experienced programming languages that have different run-times in the same programs!

Oh, but there are plenty which do just by themselves. Anything with a JIT (the more dynamic the worse) or with a garbage collector, unless the start conditions are exactly the same. Even if they are or even if your language is entirely deterministic (say it's compiled C), modern operating systems will switch the core away from the current thread to whatever else needs running, and that is not deterministic. And even if that is not the case, interrupts and varying times to handle them still aren't deterministic and effectively introduce some "jitter". And a modern general purpose CPU will have caches and instruction pipelines that add onto that.

If you wanted to benchmark something on a C64 in the 80s, you already better took the average of multiple runs. Try on anything modern that isn't a simple microcontroller, and you have to make a lot of runs just to "warm up" the system to fill its caches and so on. (There are "real time" operating systems, but those mostly just give upper bound guarantees, not full determinism.)

The question is how significant the differences are, and whether it makes sense given e.g. the system's time slices for threads.

If you change your program to calculate, say, 5 times as much, does the absolute jitter stay the same or does it seem to scale with the amount of calculation it has to perform?

The jitter seems to scale with the program.

I ran this program twice. It takes 5 hours on an emulator, so I didn't want to run it again (it is a quine of sorts btw):

Code:
EXPORT TfsUPs() BEGIN LOCAL T1:=TICKS;LOCAL T2:=0; LOCAL k:="21583203976220038516148060688225517656033562722385754869240987032029922693062590​78532034603950640129283116599690359117849735399903112570747129233682875312317892​05835996130237298147118225262010647426059454785235576471379999633800117254600048​54543280234265215099066837200237794245862711809376190179649618436959182802952202​37041845228952316300058737827900210446688422037472582472034430734586097726281982​73890816027699466337881432327067292933315026349166083528592126392194586910083878​80597923751817142265904370987299086803329053763192611045944993810172521493904422​58276587170673008834980283416650233167318207236629460500884724695346515447093112​80126128786018768121492986783700361588455325515251377625452512178661835749706726​79841277764446036183343760091605303185833169157901940867720349107875037776638549​62837757171250666974587485580511523481368095465390897606862879009181582574596402​84641750957954831492374807801690232811466715014899777596637057856445555074828701​39333359284038796239850626829573980482228116311151275465642668878881314151512851​94513913968569029306499040471322941459173098651049832354772192676326314851731450​70969379972015375279453583430696414368107380014428831765283359223803496175037367​38055463980995463155412231438359672270534144455167119533659174808219845673145105​91196551327818299328158453642211170502158448621699202744709762935173677707991201​16563425463614410897436349716991037847440326617884663645563142833099692435391900​15588165512479596467612532228323099422554020209278540539662730900814542219742089​63116055068834072455836022079898874620663575465702761516609013055843599161208664​16256091812795437299804623792264165107445761112722583480028236414706109228033191​18814308231849781281701706623023406710237626407117571495071921966256751069806965​44299715269261904557824813248162541992160948050530867778521822341662819858594163​62212519622799464532185874488412276242046256135022400298108988897003737522197590​60815993059643589546222220645951379226105246753531687825370749626016896567440747​15941209082719885817389782237860476481962847871254948795061049256298162157724337​14949662569327343726453143637623746218056900318422799639073513136165419130485441​59645601929754277564112191529324677181865699706971387924054732271138568150403606​50097502304431810674629527166720756056720055454684082553291127023826299823835689​04620668321864012899806837221289315841555693322135963985779092952571180100015918​26786371850005509985002305550668468129781233036667052711154030372404478818355490​82547303648632509536490142964742229797963690780091279593661545588190439941617660​19874166058663072009296856648124386272571741602060402218431569498702084649749380​42002679714324799664331672671038858462627700299740472686582532915616380417971366​38395795946629488826938420369065777284866905076184627057026859986122678573699698​32338015026250503540161801043238225738493486669369570372118928953686069514913736​57275209265563584684182391492011697300038606440927471463972696148460132991134795​94778338586571046391245556415295463228560006288082896485881641779590089893588563​34895026879419483324928188905077345971600832431308562758375152352939216199140752​09431068809363305985005055663521383865113295337411435605554637166727170378277292​47570250389738366212425570301947921138692193134166392992439955798478028645711996​67339532002816876308176908260182060080734316145900574651910369779099999714834898​10216749224704120495540501475682046695256122690055630957542109614292299034974271​25674797477834604383169390942453510654586364974122397500776363858887162864634949​88347860467483211158414568734014540238283340501790356220216409140330205493940485​72879739392384372278807650311745417193601199452385606629845691879672092466226356​28823154005748556118523309919240070703537660685338408120005761913296561671149826​19555125962820745688169330169881164420067494430007993166914227572084879099715999​91059449787502440627448916447600558822191496391219351785351006782450909001681221​38591042966381320086941365432144060652727706839795597575431079328037491979180401​74914931830430544170369257991358188985886062467128141550205536002529087746445851​90459038676962367199044800899096839245137549534720247936399198056772747219445889​84890832833552522313870566311245411232858178497214134292277147574149140671098902​44581591863049936573838588832521660864544178315866738605216120699085970467364434​81507757481728128906111675845627783688244361217636509035480423923141898032351554​63756793741984496654666988355044031064228602483312545936412814727981820197086161​70372879565413004811048386529386799735374894900650058625616673016523253479491582​20077761801019379607074923237977640924519207127110606449294941008025937134623778​32217465872216955672752277546965882702768907008451559296160360488018025099484727​62295313447668504588183642771390796989088598612010079196424996133037293117910551​62270706078876028946098856383903905346039546801225104546321134917287731837398697​21188240725833366336916269350308772377524389702811440234391577713171364485167180​34557248788470512893949906724310444373755164208891851336162666827858647428074222​73493962949934619173840256299124075263866522542094859106377378454276366001188631​98735967475946922670711411514128471076456897580849018714124770699041350991688175​24931835476560507910264881410025071503987022976336660331532867326567331350863128​83874440774154087288077361530711202073452963867717141198245028764483729457438256​88811501861831295342405758983530362940938420791987873554802003628992405611771857​57463953690306322905591582056770244684688612829597218426530517385942261821774470​54254513407585555883255835753258469866225751777712487488167454279593836255747373​16243167438709397143052247396794338947695349514775346188540655765033404015473726​82263837818941633840668926339840741649143428056355796972455680443412135608095303​12314668050728799539494846226333609741140553723317583566472365133434655449862997​93900639073908399912636841175213669910865139591224013297950287381596896032959021​05032649719005856324100537650570507154795084902117551805964719648750715245389019​84867400572733858541767305153119727095514586318662237021673788256452864968893248​32789785473855077289072399853703340534953221266694637592929633358535787990140498​47081379603426185525766007100393857227032160903348712096967096239997226428856604​57835707897682351450961195434293414495077779049160333679525971076575944063785804​92009158064470853767434518269585145736547370873419738271065846956169717345473752​21063285161936988639857778375529512369886385933735252637121625372356897665626635​19403653887510021109684801172443036851762509903827540704380611581413748032246319​52797085053999126265027287601782848466634494852703226316246089306164527190362282​83534838535341012084060108303114365305062631786062855148062923093217474059555966​52721055254424501310413048003982943169071617832365101982663826748084063089337934​19542102277548012106737914391726468552129702124922627011809366449341470277743706​96235531244222700514456415991192430908743492250256894419338184922442666143881440​60010187965304754864385921207835269417303689657469404914942204147786895414631360​18733992298021404440106262608242047374927813962478702260767772898325295479352562​66539066312041857852794285650800824807502810377501226224170671392243716381689112​76853422188972320635637018115570705094660378676752994600214376941966641315221019​04074100748499128637275613072864196256089356072521672515180167770814287896189050​09386944265882840132865743228082692412485749780902355573292315130053448149703457​35613220789361284264686322964371318216309795267749162725695276233298041918152124​13421507134445542371240274603394332275691278476802494106483727455338263473757188​70777774528822685723505960095020627369978439879268137727805711921363463943395939​94612573282044790996914982350458270009139323094140004867893331616538315036969285​97897498870366973644294071028056737118946389499041155287864247765608546230771184​82825195123647628201322859771742904744796889808768611669078755812908278804330536​58247093606807114765608113036174799229586456621242871867322175266372307307703279​30849234900642872712389140208305038211287558697146483646172426325942324123484877​55556807567128602604350643592613061080535445257489026191670619857047930313200649​70793224113007495186620647565180088372281427933443157602622709130120345931744303​06500281495002293448737306010323060146030564604028188950653600527217084317531529​66460327559169698155415836488379887237964004229243597343517723274277615851283909​34157509823765506094809765403975066063362073897044822535606830569387965760122296​22084129658652699164929467176373745069519885312992011572366289402714734592049587​11900620104186303054629487309663793954184042885202614656690299565099839439521682​18239206706305294403025253610604122867095274083684675221227667152607873449439045​71436167803754585416329798875527905668709389929689339304221132461185869692892784​30009346705648824242739120350869366805773431277779907169040802300833467325044028​09688443185695447344572067873931045751423330334696665922975300951130495750872167​83301598240760185090754877617246721334283652469094580646147572120281540371293274​50182171535061517639352931132418872672200706332900006555654576718242489091797248​85184726827562002665213596582331008959232600742844125349056998262408122432909689​63454720497480759734780977299020409176615087376013503561169156144803439747310973​04414553071729822179134661313927561919848280332820448752946013934449380649587110​84051154302227196468561558110347012541273091256872770777891699263443815887764062​06372564159694747819799292358654210486546585740349694259655667458984261884604741​67645552742182637956586946124804143372050615353813933258504746164128630798957171​58437546466483779487573166924493017164001611359381078024959259650880537625927425​20368639078063893418332111485815196426091810990107553327738797713769942557433791​68768008937070175862239101134546314354435662976628393221743509401675887176345395​55707758237285616204361673166920316750490964523418500407066983615286844087336696​67183771233054740863467881517021795207453234231005588809332352745474721370946969​03598640245145408452548470946159593647199540332890635402680995168986047517620252​20233521242449746023580477464450607648982727707956127669710284939691412045830601​63631681017171918203306613833436812009469611560635917814474465239126643560377038​62903988012825454755369260997925116808843266167848753979696676110628698980231874​94682995333096623212523422178960903208945970019675639801074260836896483076255067​02025643133592855968548171217481980961293009459305355483366387497877927035145696​73020905029921709228062292365405506267143864763388922122356269201244163034820278​82389503663503902656016341037135663486658238356611659642558687807493801910271855​24627376588239768764480033037634332202190224368577782263841091606208271634451128​29233339664115673145781604047943502042963115382015301752907878119094909832950724​38433664157963353283277907627195198629230995128423591495300786273600016487131266​59135659909098785727788090205778097551569203328649513200317204259616772566643396​82986952427578047518403998769099186312759847784786084943838214750435493307915507​14829495520234821324318001951488591033170712980509162332892030530371795146227052​27819778766912746008169190714939523160948379557335306438142153565922005111196322​62081053510423710350405744966357549808485069212522410135526648328266372181522201​18597304706859667250878570219573597550378144681472535820245625299248776390293377​14767397158829487962323467058470735845882779954699018505682155684224076790526445​16941195067954988235203258771185806502445164841628438759985019738881245331908367​20988204582479704305244142063834645533720696427443304267740769817173638184471671​38984068954526133706393904193062306160653862303194891463537812429129823794124546​13548100973775862659556081520772942485409596421285942918290410662188138692285794​42219218234910085481264808294295670006198524684564270143525325809635220435128149​50145824211302592527922167201353307580735836634794395073127487105345347754734957​50917073843026488654003413652104186462061773421768419452528643664700485745915395​67932144448090191994468607155934040312154608315782724105625821379741776062947665​14139906560170482717211136937280007950753875158030114831260222789794106414034884​10856470717302183954381399911028471649778816695688511875579168858073324194373374​53386710913094479785366597257228476455432898271784268768421058375584305998006570​15509704259067603626584276595558106987160361201462857445078446969290597667994030​64379276722975221416954601509406623677051488861004620488119865759969689327026069​66735768405124940048379150434203170637289876881662092551156886071986498135924585​23294797302383237648865691610332227194804579912826231339852770286301817419135144​64460109516424391760421293615773849131010307148008722048599642124805888772678303​72190066741053990560655807013514064765518778078914369223443029961150909834029853​97171778042949507668815110416898418094469847087707993535788124936276946863240725​62028464829333177697766492041747067182254797578453353675657111035998378162861076​36856341827070900268031670154559945702492240071437414426015447039427822268358673​12398525395238302564182998709364722154345166376534910821306498925468200600501559​68173927910627808984128183183098232833869641682420486244574566569368266195454005​39680249291718399026232942234532869210909028888869072912574379369688552474264148​45663167176637292442294874097111361828603398518671812387393666823447127417403283​28755348920220322397175273561373802619776984682689593424985108913930554410548080​83111443344294854722009246644217078966186356217122552111361245887551209170834039​71730977331181133047329728223753124706620337034997759633509007471535005481317146​33721187621743550736442783873731266582992450279335191000958076829985182215841800​76917765217967597022576731199942273950273325773149198119887889034630004772160079​46030055938601828246423197497784193330200079595128577122841800576971182729946710​12949597615417895581466841980325565792566416093098388099073016499440615664095047​75377589013292891495319903125868962713254934282488476236301702937031991424680321​96902792167327228621008095081597594645755592986796376383196071670268828924681239​75186003774911267290608858643096421261986810369508567215981092305755034271287485​19779932913570545711017099413330218806058424186938396108309955187208181503682857​08002278640558773314866804258855188484452192235572968366218855837060625524186873​65759180592257340735392520313818339102505052444383517924404412534717220968206887​24557533999833983574788419615741610658938180687639768458606908290443495810779312​60644577338531740308817779011188370074475847331128910518678277016564946797590528​82392955315262076816173226347728136687666914803432580961726185747549724577909175​15676623693921373785758457359825602398721144064401882617660694546349151847260432​78685985896307099310498701959093343317828195071307967766743129516031761194374531​94816803243398143353642611537994597165334549812297980534448272138193099410154340​66475700462331761055670372481175014421335758397533123845422822214894007193491128​97834036585298232289452925134357489495202577867150209032332807823070736854020892​92470155312923522974292926468644900563720022417994434334890276496598186521265218​95231826703253318551728288255653687546376537747216474938698595515204321617902197​92804451274176482278822964103520035366281178452560558659570377055386714260467960​59501437592520733043401050895052621929844836876259896515857716974468156344426782​44688942034979744857426331224540475367074905280853446276343304176743365514927061​52857978432087965182258913612757799102863025289772519234317368553159678088550375​32126025661241415662683061170072068920362014619797162234601331972550816819051725​80765617047931974822571735594987659549866110715157973201962386679350314159580523​44030418934817392200132216852968179798216372333181806055632393422457833243373673​91155376917348914920041073128456544990131560120285796196732947014294447472056642​22625614133339004037844620481365990970911970117078275249449039682915266103344614​90954733638922037541950909299733046952065372729827933943991223077328545037051529​51382079955751388192880981972675093818879181805034339884788446728343706031244356​56983667767437368547831044728618839100186120922918355723876234813488915185254290​28127984166743602133385300090861664213417153840300357355503178149233380092481113​95841543370223747012494680752517259320717647613603244010007336422153752365120636​41909968498255959892291436673284969847482574140675679044821462787848514160667624​81751455124433505853953001929605384671865920797097839115708538833304284315012511​80426695901079859460626121827714262405923289037686352051837876759997141640920511​49709915999501092243821182944008978368611280891106295118963296566137900163597313​63806413508836113687827268595734312165451427155000168564432819336614257523577868​88490666095747901574079566692877710576689602429337169355925162204418203069020740​05107735679341901938750647651133070542629225424875841202279541104634066285369075​69418709944334600574857706500319051729878483146075228996482543267544112028859232​67990910023525466452920806034930153165463351921313792865507525345497085052245408​93533000585945906696868478547244493253006250589976538220504110210114453698734558​00976144099993432453693138560209524328026062819579040343414745711741460425391959​78209228732224536303843772516463967109317487995192102509146379093976076364774213​47569600827825983421713571414718995136857902843840633695985964653860279966730914​24744044161451643449128417017097293253754469586875318126747173424969997665608321​86918494415578798359210323680173771022562468918527917898822734518579272700907377​91494275248754121196901791702753118852110585267072886964155747689525248017667136​83583271393090631442591125955761347985502125976121688789389722184383545294492152​98882312577952575766749670907254386787556167155428611575218987988514822229503889​65653003206310131419367026831340101526761691358597178182023604668537405340828834​26403510010214375352004582764862968131901928122382441950043407371276523580794618​05038190755678147169349846790343644627282185627172172462131111762501986397379627​23188647341036355604847968820700102538326834822886531615300946884532474083095082​66189709894649858906199596765759247648964781186053933296262842685366123616704052​80931156977825188062015120602819059038378866714603642952624868496162740569343588​85070102078905626832020083260208058144114164012184067192216130698369829922409042​76512008131509438669522352200266369584420869394643100850167831632667611305218154​80069490471175744892201662808729148057362561981455811121395942544368644327616557​44065207710260201649194436915531102014905965747546198182423340338922259769012699​34430977358936295073823235323379450012731572630847421177575662709716366108646663​80616307421643912325779144207270194525729268974042083162370784322218759546151786​62628055153630973804334796673512524301179697504998210051577225904081018841058689​28341251792521795804460805502322543950393158548569677038693584626985283811199018​16434535522307160049324147301190592402195798449380518453550393803219349989208354​72722693758222831667926514966615203261621368660276707009406488137162343253699483​60430930877415515186268768610785694860428957689829289313393834975097955263665999​20084621688147856747182556296540411417622087012120327838256535490213059346048113​01080445875634259680553174733989153728375261620059560120801370754116187758914489​76107753965170026193423967905346127434097735349906439950028504208654805659835047​38988554643820455050202779459239922859319683313496546912320472195669187429694918​70473389870400867800735463266603371394475476095749253680296361978343587844790409​30765649453002611507709534684943893775249971445267211128601121661803463122586823​60917063410948246477260865485201498773889748265804175308893006668795562002165353​82695466292240840332528688490842618308637750997256838207321959378718189019561946​35555012023432892615582567313025133607372353235940566450416725005284115429189744​99067172964048810057590336008970815423018773617723592351882808288422427847984658​75835537999269877976544120977460532995072950455222860631901343090390322587504985​38917540927908623581979818521822795903547091657249773469145686538037203984690528​20816263400169764699746807736519874989638742582209581636775866232730120512815179​23855122308997535135166455489353075958465837309071756183133137078494976962338100​06881171481658923176787878740360516153761245834887757928594817826605214377687757​12539351231735070707691315857968189743507424450326450198591725034048401264533391​43994609078764024101614151424819652626006386930471937590086263301564232641546791​66488969898794292883126211370731346377591778969962603088094101441091612594613272​25650742668875165742485148878184753761652634410481416191825618828798127688093250​42570239961922805322688065164302454289465245731672623411720741198905771707987387​58232342026114944065991156846950144247527058675601633947775088977775795393792682​66153148977159104888787508279343464773106196547617924491925311627473680125730298​47941221285380089059840909377349250515694534240718137673803266883527875863992714​53712264018288527415120959692985210772201345438267390979985706981382905762535422​83312622814612137700608641500314897090572414606576383925633641863237941756749167​28954133881969850189870675480749564072873486810084830047097237770617508713747612​72006169332985765265134930991963090583224635578930916279974840495145227932929040​10203905630624421777293185736975333418296943704324396170327110565414137702929562​45905461705432092256648626214902208304975377261012332068003289736087060856052846​01951547818300062139080908262106116354966827013625741085728381392614457675624403​78763688698115141953451068012983666330137755353770423375656772964190333385672017​13239902916075802378701468479990478851519769565390365519508605318181471515358533​39369683843848668749468364221548793435936409161649033434693619624429221283699537​10929508477686255695753022307125464719253559055044693409214033314819537111218085​87490764041845635192267563657735175358186601363428660824828736122139667936352164​03223932758936876153037576035200253110831093519119361061818107989247268368183565​82011893087781046383257062986112365762084775108100118144986747026781292106848559​50225826364363572972576265239645610950227272291805338292865945229865994851270901​07739555939889372538779984712739723710559635270902072157687633687583258111020849​16298985021050513422814452413024595072581967734724855242584820796844089198247005​91273103944419181743612207547952681789150663787794204196713408604777050164427053​97207245667224284381944784922754203336938417904720011473777717332010833814020972​50046809185708233982161959451227579833283420829131623208931475467415361700496961​45949328550530701759903328190038766922963352150491495530631936662880072181367558​33230785227630023464494116424088060842916133644053393603481582216552139932789460​90510121212693196273406936917508079016471905768809304264260708895800063684915358​88815796193110913718349695486502102164197649199345164228521875559314893418444354​83978776895683967045738089548053062090236102829108478731876949526346386384361236​48651484542607738785499898341056408372259519230206322281198410426944430891727222​66494500779861120402078393003246705892589238534233504445319778489859085662630705​5";        //   Divide long number by 17 and store by using  LOCAL A:=0;LOCAL B:=lsd(k);LOCAL C:=0;LOCAL D:=0;LOCAL E:=0;LOCAL B1:=B;LOCAL B2:="";LOCAL B3:=B;LOCAL NA:="";LOCAL DONE:=0;LOCAL CO:=0; rect(); REPEAT    B3:=B1;   REPEAT     //  Take long integer and turn it into binary digit by digit.    IF B1(1,1)=="0" THEN      IF length(B1)==1 THEN           DONE:=1;      END;      B1:=RIGHT(B1,length(B1)-1);    END;   UNTIL B1(1,1)≠"0" OR DONE==1;   NA:=0;     FOR P FROM 1 TO length(B1) DO           C:=EXPR(B1(P,1));           A:=NA;        IF C MOD 2 THEN           NA:=5 ELSE           NA:=0;        END;           D:=FLOOR(C/2);           E:=D+A;           B1(P):=STRING(E);     END;     //  Plot each pixel as it is met.     IF EXPR(B3(length(B3),1)) MOD 2 THEN               PIXON_P(FLOOR(CO/240),(CO MOD 240),0);     END;     CO:=CO+1;     UNTIL DONE==1;     T2:=TICKS;     PRINT(T2-T1);     WAIT(−1); REPEAT WAIT(-1); UNTIL ISKEYDOWN(4); END; lsd(number)  //  LongStringDivision will Divide a long string integer of ANY length by a    //  Number that the calcualator can handle, integer result only. BEGIN LOCAL divisor:=17; LOCAL ans:=""; LOCAL idx:=1; LOCAL temp:=1; temp:=number(idx)-48;   WHILE temp<divisor DO       temp:=temp*10+(number(idx+1)-48);       idx:=idx+1;   END;   idx:=idx+1;   WHILE size(number)>idx-1 DO      ans:=ans+STRING(FLOOR(temp/divisor));      temp:=(temp MOD divisor)*10+(number(idx)-48);      idx:=idx+1;   END;   idx:=idx+1;   ans:=ans+STRING(FLOOR(temp/divisor));   IF size(ans)==0 THEN     RETURN 0;   ELSE     RETURN ans;   END; END;

And it took, 1st try: 19,391,369 ticks then 2nd try: 19,530,835.
05-25-2022, 03:51 PM (This post was last modified: 05-25-2022 04:09 PM by Albert Chan.)
Post: #10
 Albert Chan Senior Member Posts: 2,066 Joined: Jul 2018
RE: Varying speed of a program.
To speed up, we could do in-place divide, instead of building up quotient, 1 char at a time.

This eliminated cost of generating intermediate strings, and cost of garbage collection, both O(n^2)
(Timing jitters likely comes from unpredictable timing of garbage collection.)

With positive integer divisor, dividend has enough room to hold the quotient.

CAS> s := "12345"
CAS> s[1] =< "0"    // in-place string update
CAS> s                   → "02345"

s is treated just like an array, with INPUT showed array_sto("0",s[1])

Instead of removing quotient leading zeroes, we could track index of first non-zero entry.

Comment: we might as well work with true array, saving cost of str/num conversions.

HOME> L1 := ASC("12345") - 48      → {1,2,3,4,5}
HOME> L1[1] := 0                           → {0,2,3,4,5}
 « Next Oldest | Next Newest »