Ten commandments for code optimization (alpha release :P)
I have been reading, studying, googling about code and SQL optimisation. There are books on this subject. Following is the crux of everything. I will try to compile a list of tips for PHP, JavaScript, Apache and SQL optimisation too, hence the beta status.
The following list is applicable to any (God Damn) Computer language.
I. Thy shall never calculate or evaluate an expression at every iteration.
Following is a sin
for(i=0;i<sizeof(thelengthyarray);i++{
//do some thing
}
The same applies to SQL too. If you just need id and name from a user table don’t use SELECT * in fact use SELECT id, name FROM users. And this is not all, performing calculations on column name too slow downs the query. Avoid this too.
II. Thy shall avoid initialise and then assign. Directly assign. Save call to initialisation.
III. Thy shall avoid too many elseifs, use switch. Put the most common cases first.
IV. Thy shall avoid post increments and decrements in side a loop expression.
V. Global variable is an act of adultery. In fact avoid declaring variables as much as possible and declare variables in inner loops. They will be out of scope immidiately.
VI. Thy shall make local functions static.
VII. Thy shall avoid expensive operations.
Interestingly addition is faster than multiplication. But if you need to add first 100 integers use a mathematical formula. Why? Coz’ it will be cheap.
VIII. Thy shall always cache the data required frequently.
IX. Thy shall always “kill” the unused variable, claim the memory occupied by these and return to the system.
X. Thy shall never reinvent the wheel.
Don’t write a new sqrt() function if it is already there. RTFM. Language constructs are better than your own home baked functions.
Breaking “News”
News channels now a days are providing breaking news that makes me think why they are breaking news. Recently Mumbai administration warned news channel against showing file video clippings of 2006 floods without labeling them as file. In recent times level of journalism have dropped to lowest. With electronic media always looking for some “breaking news” every news which is of no use is a news now. My colleague have sent me following images to prove more.
![]()
![]()
![]()
![]()
![]()
Click these images to view the full image of “breaking news”. Apparently, ironically, (un)luckily the channel telecasting all these “breaking news” is our own, the best, award winning, none else, “Aaj Tak“. I have got loads of things to say for our favorite news channel. A pre historic post regarding this was supposed to appear on a blog too.
CSS Diagnostic script
I found the following piece of code here. I pasted the stuff in one of my recently done layout and found that
- don’t leave alt blank, put some thing there
- height and width must be controlled using CSS only, I knew this is in context of tables and divs but wasn’t aware of the fact that this rule applies to images too, d’oh! silly me
/* Empty Elements */
div:empty, span:empty, li:empty, p:empty, td:empty, th:empty
{ padding: 20px; border: 5px dotted yellow !important; }
/* Empty Attributes */
*[alt=”"], *[title=”"], *[], *[id=”"], a[href=”"], a[href=”#”]
{ border: 5px solid yellow !important; }
/* Deprecated Elements */
applet, basefont, center, dir, font, isindex, menu, s, strike, u
{ border: 5px dotted red !important; }
/* Deprecated Attributes */
*[background], *[bgcolor], *[clear], *[color], *[compact], *[noshade], *[nowrap], *[size], *[start],
*[bottommargin], *[leftmargin], *[rightmargin], *[topmargin], *[marginheight], *[marginwidth], *[alink], *[link], *[text], *[vlink],
*[align], *[valign],
*[hspace], *[vspace],
*[height], *[width],
ul[type], ol[type], li[type]
{ border: 5px solid red !important; }
/* Proposed Deprecated Elements */
input[type=”button”], big, tt
{ border: 5px dotted #33FF00 !important; }
/* Proposed Deprecated Attributes */
*[border], a[target], table[cellpadding], table[cellspacing], *[name]
{ border: 5px solid #33FF00 !important; }
made “foreach”
This may sound obvious but I still want to mention it ![]()
<?php
$a = array(
1=>'B',
2=>'C',
3=>'D',
0=>'A',
4=>'E',
);
echo "Using for\n";
$c = count($a);
for($i=0;$i<$c;$i++){
echo $a[$i],"\n";
}
echo "Using foreach\n";
foreach($a as $k){
echo $k, "\n";
}
?>
Output is
Using for
A
B
C
D
E
Using foreach
B
C
D
A
E
Loser
In the year of 2006, through this blogging world I came in touch with many wonderful people. Some wow, some ok ok, some crazy and some losers. This post is about a particular loser. This lady, yes a lady, lets call her “V”, used to blog about love stories. Those typical girly stuff with a prince coming and blind faith in GOD and so on. She ones copied one of my post and didnt even link me back. Then she was on my Yahoo! messenger. Things started going wrong way in her life due to silly posts on her blog. She left her job/was fired/sacked/what else I dont know except one thing that she was job less. I tried to help her. Burnt a CD, couriered her, and called her, so many things which I don’t remember now.
During all this time period she din’t know I was aware of her age and even have seen her. >
She was job less and then after some time moved to her home town. Miss V returned to battlefield to get a job. This time I again tried to help her.
I was sick of her silly stupid stuff. I dont know what went into her head she called up one of our common friend and said, “Kumar said I will get you a job but you will have to spend one night with me” . ![]()
![]()
WHAT???!!!!
WT#????
This was enough. When our common friend told me this thing I was standing at a sweets shop. I just called her and yelled at her. Everyone in shop was staring at me.
Anyway.
Now as I am moving to Chennai I wanted to contact one of my friend there. She was Miss V’s colleague at one point of time. I was not able to trace my pal and so after a while I thought may be Miss V will pass my message to my friend and then my friend can call me. So I sent her a mail
Hi “V”,
I need a small help from you. If you are in touch with Sumathi can contact her? I need to speak to Sumathi. My cell phone number is 9878622586.
Thanks a lot.
–
Regards,
Kumar Chetan Sharma
and here is her reply
Dear Mr.Kumar Chetan Sharma,
You have hurt be badly by ur actions, words, mail, blog. For old time’s sake i am answering now.
Can u put a post like this in ur blog?
Dear Vani,
Pls forgive me.
ur friend
Kumar.
Just 4 lines. If u put this then i wil answer to ur mail. And give me ur url once u put this post.
Thank You,
With regards,
V
Oh boy, I read the mail and I was laughing.
. What is this lady? I mean I dont have any words for her except loser. Now I have posted those 4 lines on my blog. I will send her the URL and I do expect her “remarks”. I am still laughing.


