Archive - Jun 12, 2007

Notes for June 12

in

Revenge is a dish best served with red wine. Wine have antioxidants, antioxidants are good for you after some rage.

If there is rain, the regents will be canceled.
Oh, wait, my bad. that only happen to good things like field trip and sun tanning.

I got a Vegetarian starter kit.
Propaganda!
Some radical Vegetarians demands a federal law to protect the animals we eat.
This event completely support my idea about religion. When does animals become US citizen and can take part in voting? Do you support the act about pigs can drive cars... when the insurance is from progressive? Ridiculous communists who wants to take away capitalism and democracy.

How do you loop though an non-associative arrays?

Often, I see people loop though non-associative arrays like this:

for($i=0;$i<count($array);$i++){
//do stuff to $array[$i];
}

List of my initial thoughts

so I would rewrite it as

 $count = count($array):
while($i<$count){
	//do stuff to $array[$i]
	++$i;
}

Ok, now many people would start moaning about how while loop is evil since for loop is easier to understand and takes less lines. use for loop if you want...

Usually, I would stop optimize since I can't see anything that can increase the speed. This morning, I had to test one of my thoughts about loop the array backward, so I got something like this:

 $count = count($array):
$i = $count;
while($i){
--$i;	
//do stuff to $array[$i]
 
}

This is the most optimized loop yet, one less comparison ($i<$count)
BIG DEAL!
benchmark the non optimized code with the last one, loop though 10000 key array. The non optimized loop is 3 times slower than the most optimized version.

Honey Pot that kill bots