MILLISEC is a millisecond (1/1000) timer. It uses the 3DS's internal clock, which runs constantly.
MAINCNT is a counter, which increments during screen refresh (59.834 times per second). Since this is controlled by SB, it doesn't happen during sleep mode or when you're on the home menu.
When you go to the menu or close your 3DS, they become desynchronized, which is easy to detect.
VAR DIFF#=(MS%-_OLDMS%)-(MC%-_OLDMC%)/59.834*1000 '3DS runs at 59.834 FPS, not exactly 60
IF DIFF#<2 THEN DIFF#=0 'ignore differences of less than 2 milliseconds
ENDThis function does not need to run constantly. It will return the number of milliseconds spent in sleep mode or the home menu since the last call.