Re: Anyway of knowing whether a integer is odd or even
| From: | David Otton | Date: | Wed, 10 Oct 2001 12:39:00 +0000 |
| Subject: | Re: Anyway of knowing whether a integer is odd or even | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-70643@lists.php.net to get a copy of this message | ||
On Tue, 09 Oct 2001 16:18:09 -0400, you wrote:
>Is
>
>if ($theInt & 1)
>
>really that much harder to read than
>
>if ($theInt % 2 > 0)
>
>?
This is subjective of course, but... yes. "(x%2)" is the most common
way to derive odd and even numbers. Manuals and tutorials use it,
prime-sieves use it. Bit-masks aren't in common use outside systems
programming.
The programmer that has to follow you 6 months down the line doesn't
care that you shaved a few clock cycles off by being clever... he does
care that you left him clean, easily readable code.
MOD is also the more general solution - how would you change the code
to colour every nth line red?
(BTW, a quick test show that 1 billion (i&1) in a loop execute in 9
seconds, while (i%2) execute in 12 seconds. C (not PHP), MSVC++6,
PIII800. Which means I gain 3 billionths of a second on each test if I
use the bitwise operator.)
>And why wouldn't you want to get the most performance out of your code,
>regardless of the language, scripting or otherwise? Especially when it's a
>one or two character switch that makes the difference?
Because programmer time is /vastly/ more valuable than machine time.
Do you remove comments from your scripts because parsing /* */ //
symbols soaks up CPU time?
>I doubt that PHP should be classified as a scripting language any more
>than Perl is. Scripting languages are usually used to glue different
>applications together, like a shell script that uses awk and sed or
>whatever. PHP can be used to build applications, so it's more than a
>scripting language. Interpreted language is more like it.
"Scripting language" is a common-usage synonym for "interpreted
language", and has been for several years. ("Your search for scripting
language found 387 results on www.perl.com.")
http://www.tuxedo.org/~esr/jargon/html/entry/Python.html
Not that I don't agree with you...
djo