BBC BASIC for Windows
Programming >> BBC BASIC language >> MOD vs AND
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1412959873

MOD vs AND
Post by Matt on Oct 10th, 2014, 4:51pm

Hi,

Just curious.

I know there's a technical difference, but is there a significant speed difference between the use of MOD and AND? The rudimentary test below indicates a slight speed drop when using AND.

Code:
      T% = TIME
      FOR I% = 0 TO &FFFFFF
        A% = I% MOD &10000
      NEXT
      PRINT TIME - T%

      T% = TIME
      FOR I% = 0 TO &FFFFFF
        B% = I% AND &FFFF
      NEXT
      PRINT TIME - T%
 

When using binary related figures, such as in the case above, is there a specific advantage to using AND rather than MOD? Many times in the illustation programs and code snipits from the various helps, I find that AND is used, where MOD might be used. Is this just common practice?

Matt
Re: MOD vs AND
Post by rtr2 on Oct 10th, 2014, 5:31pm

on Oct 10th, 2014, 4:51pm, Matt wrote:
is there a significant speed difference between the use of MOD and AND? The rudimentary test below indicates a slight speed drop when using AND.

You seem to have answered your own question: you measured a "slight" difference; whether "slight" is "significant" depends on the circumstances, but I would say usually not.

Quote:
Many times in the illustation programs and code snipits from the various helps, I find that AND is used, where MOD might be used. Is this just common practice?

I would speculate that it's because on some older platforms which don't have a native division instruction, like the BBC Micro or BBC BASIC (Z80), you might expect MOD to be quite a lot slower than AND.

Richard.

Re: MOD vs AND
Post by Matt on Oct 10th, 2014, 6:51pm

Thanks, Richard.