Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
Hey Microblocks, is it ok to modify your routine to eliminate the many "int's" and use "\" instead? Or in other words:-
[code] function DayOfWeek(year, month, day) a = ((14-month)\12) m = month + 12*a - 2 y = year - a DayOfWeek = (day + y + (y\4)-(y\100)+(y\400)+(31*m\12)) mod 7 end function [/code]
Although I guess now looking at it, it's a case of '6 of one, half a dozen of the other'......
GTG! ...... Don't worry mate, it'll be GoodToGo!
MicroBlocks
Guru
Joined: 12/05/2012 Location: ThailandPosts: 2209
Posted: 12:54am 04 Aug 2017
Copy link to clipboard
Print this post
It still swaps strings around though and that depending on the size of the strings can cost time. It uses a lot of overhead in that case. The way to solve that is to create another array with integers, fill them with the numbers from 0-number of elements. Then each time you need to swap a string, you swap the integers instead. This is a lot faster but is only worthwhile when using larger arrays. Depending on the data and on how the interpreter handles the data internally it can make a huge difference. Only a test will tell.
Microblocks. Build with logic.
CaptainBoing
Guru
Joined: 07/09/2016 Location: United KingdomPosts: 1986
Posted: 12:55am 04 Aug 2017
Copy link to clipboard
Print this post
I played with this but even with a 5MHz clock, the time difference was negligible so I left them to remain faithful to the original.
timer=0 x=dayofweek(2017,8,4) ? timer,x
timer=0 x=dow(2017,8,4) ? timer,x
Function DayOfWeek(year, month, day) a = int((14-month)/12) m = month + 12*a - 2 y = year - a DayOfWeek = (day + y + int(y/4)-int(y/100)+int(y/400)+int(31*m/12)) mod 7 End Function
Function Dow(year As integer, month As integer, day As integer) As integer local integer a,m,y a = (14-month)\12 m = month + 12*a - 2 y = year - a Dow = (day + y + (y\4)-(y\100)+(y\400)+Int(31*m/12)) Mod 7 End Function
Edited by CaptainBoing 2017-08-05
Grogster
Admin Group
Joined: 31/12/2012 Location: New ZealandPosts: 9072
Posted: 12:59am 04 Aug 2017
Copy link to clipboard
Print this post
Confirmed. No problems to report. Works correctly with any date I throw at it.Smoke makes things work. When the smoke gets out, it stops!
MicroBlocks
Guru
Joined: 12/05/2012 Location: ThailandPosts: 2209
Posted: 01:25am 04 Aug 2017
Copy link to clipboard
Print this post
Personal preference is to use int() instead of the \. I just find it more readable. In C# and Javascript which i use most it is optimized away by the compiler anyway. In Basic the INT() is tokenised so also not much gain. The brackets also neatly and clearly show which value is used. When having a expression within those brackets it is clear that the INT is done over the result of that expression. In the example you can see that clearly, the INT(31*m/12) is more difficult to replace as you will need to consider the precedence of the operators. I also never use i++ but instead use i = i + 1; Also a+=1 or a*=5 etc i write as it is done in Basic. Much clearer and less chance for mistakes. Also keeps he syntax mostly the same in Basic, C/C#, Javascript. Again compilers/JIT optimize it. Edited by MicroBlocks 2017-08-05Microblocks. Build with logic.
CaptainBoing
Guru
Joined: 07/09/2016 Location: United KingdomPosts: 1986