|
Posted by John Smith on 10/10/81 11:23
Andy Dingley wrote:
> On Mon, 08 Aug 2005 10:44:03 -0400, John Smith <noone@nowhere.net>
> wrote:
>
>
>>Furthermore, if you use "mod" instead of "and", you are doing the
>>equivalent of a division operation on every loop iteration.
>
>
> Do you have a hat ? Because if you do, and this makes the slightest
> difference, then I'll eat it.
>
>
>>"Mod" takes dozens of CPU cycles whereas "and" typically takes only one.
>
>
> Agreed. But it's the 21st century - we have CPU cycles to burn! In
> amongst all the many tasks going on to output a page, I will be _amazed_
> if the overhead due to mod vs. and (which I'm sure is real) is anything
> like significant.
>
> Personally I'd stick with Mod, not And. This is for three reasons:
>
> - It's clearer. Code that's self-explanatory always beats code that
> needs a comment to explain it.
>
> - Code with Mod is easily changed to put the stripes out every 3 or 5
> lines. And isn't.
>
> - And relies on "And" meaning a bitwise and on that platform, not a
> logical and. That's a big assumption to make for long-term code that
> might get ported.
<%
' http://www.arcticlabs.com/
Set objStopwatch = Server.CreateObject("Toolsack.Stopwatch")
objStopwatch.Start()
i = 0
For i = 0 To 1000000
'j = i Mod 2
j = i And 1
Next
Response.write FormatNumber(objStopwatch.Stop() * 1000, 10) & "
milliseconds"
%>
I did a little test to compare "Mod" vs "And", and I found no difference
in execution time for one million executions. The above script takes
about 915 ms on my system regardless of which line is uncommented. This
may mean the overhead of processing the individual line of script is
much higher than the difference in cost between the two operators.
Sometimes when one agressively tries to optimize code, one makes changes
that should work theoretically, but practically have no effect. I based
my experience with optimizing some C programs, where "mod" had a big
effect in some tight loops versus using "and".
In ASP, of your three reasons, I think number two is most compelling.
Navigation:
[Reply to this message]
|