what is the actual use of these scripts? I mean, they seem (to my inexperienced eyes) self-contained, i.e. not a function or anyway something that can be called from another script?
I see that you are using the "other" algorithm using bitwise operations to find out if a year is a leap year, not the most common one derived from:
Every year that is exactly divisible by fouris a leap year , except for years that are exactly divisible by 100 , but these centurial years are leap years, if they are exactly divisible by 400 .
Was this choice made because of some reasons or only because you fancied it?
With ($yr&3!=0) the condition is true if the value in $yr is not exactly divisible by 4.
The part ($yr&15!=0)&&((($yr/25)*25)=$yr) tests for when the value is divisible by 100 (by 25 and by 4 because if not by 4 then the previous ($yr&3!=0) is already taken) but not by 16 (which means value is also not divisible by 400).
So, this whole (($yr&3!=0)||(($yr&15!=0)&&((($yr/25)*25)=$yr))) is true if the number is not divisible by 4, or divisible by 100 but not by 400. Which is the inverse of
Normally when we are dealing with native code, then BITWISE AND is of course faster than an integer (modulo) division. However, we are talking from an interpreted script language here, and the tiny amount of time saved by the bit operation is probably overshadowed by order of magnitudes (hundreds of times) by the time spent to parse the extra characters and interpreting the resulting internal representation.
Yep, I thought so, even because it is not like you are running this set of operations in the thousands per second.
The given thread on stackoverflow has results of a nice test (but on compiled C) about the speed of the various possible approaches, but obviously chosing the one or the other doesn't change anything in practice.
This said by someone that - to the bewildered look of many people - usually multiplies by 355 and divides by 113 to get Pi when calculating a circumference or the area of a circle .
Yup. Imagine all the string parsing so outweighs any "optimized" RouterOS script.
Now, all scripts get "trans-piled" into an intermediate language (IL) [or perhaps p-code or s-expressions be equally valid analogies). This is what you see in code datatype (like /system/script/environment or /environment/print) – this is done when you create a function variables, or save a script. And, this is why using :parse or :execute is significantly slower than functions/stored scripts in my observations – since it has to convert the string-based, user-facing script into the "IL" codeeverytime a script that runs using script-as-strings (vs when saving/storing a script).
Given that, and linear string parsing, using less characters is #1 way to making things "faster" - if one cared even though likely useless in nearly all practical applications for RSC. But to this point, one monolithic block that deals with both V6 and V7 is likely slower than having 3 functions: one that figures out V6 vs V7, then one function for V6 and another V7.
You should use a colon in :find to ensure you're not getting some command's find matching operator if somehow path was not root. And one could quibble about unnecessary semi-colons, too