Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 10:54 09 May 2024 Privacy Policy
Jump to

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.

Forum Index : Microcontroller and PC projects : get week day function memory issue?

     Page 2 of 2    
Author Message
GoodToGo!

Senior Member

Joined: 23/04/2017
Location: Australia
Posts: 188
Posted: 12:34am 04 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said   I posted this in Januari 2014 and i have been using it since the GWBasic days on the PC, so it should be time tested.
I use it often in programs as one of those handy to have functions.
It is a bit cryptic, but it is small and FAST. :)
[code]
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
[/code]



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: Thailand
Posts: 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 Kingdom
Posts: 1986
Posted: 12:55am 04 Aug 2017
Copy link to clipboard 
Print this post

  GoodToGo! said  
Hey Microblocks, is it ok to modify your routine to eliminate the many "int's" and use "\" instead?


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 Zealand
Posts: 9072
Posted: 12:59am 04 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said  I think Grogster uses it also (see his post) and maybe he can confirm it works good. I only tested it for current dates, and in that case it works perfectly.


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: Thailand
Posts: 2209
Posted: 01:25am 04 Aug 2017
Copy link to clipboard 
Print this post

  CaptainBoing said  
  GoodToGo! said  
Hey Microblocks, is it ok to modify your routine to eliminate the many "int's" and use "\" instead?


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.


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-05
Microblocks. Build with logic.
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 1986
Posted: 01:46am 04 Aug 2017
Copy link to clipboard 
Print this post

  MicroBlocks said  
the INT(31*m/12) is more difficult to replace

heh. you noticed that
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024