02 February 2010

Optimization VS Accuracy

Assalamulaikum wbt..

Ok come back to my world - programming. Seperti tajuk kat atas tu, hri nie aku nk cite sikit sal pendapat aku, ape y aku rase bagus n ape y aku rase xbgus utk kite selaku programmer buat especially those who are C# beginner like me.

"OPTIMIZATION & EFFICIENCY"

Sedar atau x, setiap line of programming of code y kite buat sebenarnye mempengaruhi kadar efficiency program kite as a whole. Aku bagi contoh utk buat 1 application, programmer A memerlukan 1000 line utk siapkan bende tue tp pd ms y sama, programmer B hanya memerlukan 300 lines jer. Jadi y mana y lebih efficient?? Pada aku, efficiency sesuatu programming code tue sebenanye terletak pd faktor turn around time dan memory consumption. Memang jelas kalu compare based on number of lines, programmer B mmg power, tp kalu application die buat laptop aku jd slow nk mati, xguna jugak kan. Kesimpulannya, sbb tue la kite semua selaku pelajar CS wajib amik CPT212 - Algorithm n Data Structure. Memang aku akui, aku mmg kagum gile dgn org y bleh develop sesuatu algorithm nie. Xtaw la baper byk sel2 neuron y berhubung dlm kepala die. So kenapa turn around time n memory consumption??

"TURN AROUND TIME"

Apekah die turn around time?? Aku bg 1 contoh y plig simple. Bile kite bukak page Yahoo n register akaun emel baru, brapa second y die amik utk mksure registeration kite succesful?? Kalu kite wat Transact-SQL n select some table from MS Sql, brape lama ms y diamik utk load all data kat interface?? Sumer nye berkaitan dengan masa kan..Pd pendapat aku, one thing y every programmer should think about is how long it should take to do the specified task. Aku penah baca buku Human & Computer Interaction (HCI) oleh Stalling kepunyaan Umi Mahmudah Mohd Jaafar (kakak aku) y kini sorang network traffic consultant for streamyx asia pacific. Dalam buku tue ade sebut sal 1 prinsip paling utama dlm design GUI - Respond must be given within 5-7 seconds. Dgn kata laen, lebih dri tue, user akan rase bengang sama mcm korang cbe connect USMHotspot xdpt2.

Aku bg 1 contoh implementation y mungkin akan bg serba sikit idea pasal reducing turn around time nih. Usually, ms pling byk dibazirkan dlm sesuatu application adalah utk load data or set data dlm database..Kene plak network slow, mmg cm taik ar. Setiap programmer ader cra msing2.. Tp kat sini nk bg contoh cra aku utk memudahkan pemahaman.. Besenye aku akn wat system aku xkire la web or desktop based utk pre-loading dlu sumer data2 y diperlukan n fill in dlm 1 container cam linklist, dataset or data table. Contohnye kalu application tue sal hotel management, aku akn load awal2 sumer info sal customer,registration and soon dlm 1 dataset or text file so that next time aku dh xperlu lgi access database. Hanya bile berlaku update or delete je bru aku access database n fetch blik new data. So, xderla user tue tekan 1 button, then system access database, tekan button lagi, access lagi, tekan then access lgi.Brapa byk nk access daa. Load jer sumer data awal2, store lam temporary container cm dataset, then bile nk access, access jer dri dataset tuh. Advantages cara nie ade 2 iaitu reduce number of connection y perlu kite wat (user toksah tunggu lama2) n application still works even internet connection gone.Best kan..How to do it? byk cra die, depends on what your task needs..

"MEMORY CONSUMPTION"

Haa part nie y aku plig suke nie.Penah x kalu korang wat website or desktop application then bukak task manager tgk braper byk memory consumption application korang?? Mesti xpenah kan, sbb y kite taw ialah janji siap n dpt submit on time. Actually bnde nie penting gak sebenarnye sbb not every user or customer y akn gune application kite nanti ade 2 or 3 gig RAM n Dual Core. Kite selaku programmer, kite kene la consider dorang jgak kan. Dorang pn manusia jgak mcm kite..Sbg contoh, software iNetmon utk dak2 network CS bleh capai 250++ KB total memory consumption n membuatkan laptop aku kdag2 jd slow especially bile turn on sumer module2 die. As a comparison, Office 2010 sometimes hanye dalam 70 - 100 KB jaa n kalu korang tgk, application dorang smooth jer. Senang hati nk menaip wat assignment kan..Dengan kate laen, kalu la korang bleh reduce memory consumption nie ke tahap membanggakan, insyaAllah application korang mesti super duper laju.

Aku bgi contoh la supaya korang mudah paham. Besenye kite declare variable kan. Ntah mane2 kite declare. Asal nk gune jer declare baru. Sedar or x, setiap declaration y kite wat akan menambahkan penggunaan memory. Camni, bile kite declare, system akn sediakan 1 memory address space utk variable tuh n lock utk kegunaan application kite shj. Once kite dh hbis gune, memory address tue still there n lock kecuali kalu kite dispose or null kan variable tue. Cemana aku nk explain erk. Let say la korang ade 1000 lines of programming code. Declare ar 1 global variable type string n gune jer variable tue as a temporary to store anything through out whole programm whenever needed. Jimat memory space sikit. 1 lagi adalah penggunaan List dan String. Cube compare declaration kat bwh nie:

string[] TempString = new string[100];
list TempList = new list();

Bile declare string, kite kene specify size die tul x? Belum tentu kite akn gune sumernye sampai habis. Bile declare jer string array, system dh sediakan n lock 100 memory space utk kite. membazir namanye tu..Xelok membazir ni, kwn syaitan..hehe.Solution nye, gune la List sbb list nie aku bleh kata cemana erk, ermm "memory on demand" kot.Sbb bile kite declare, xperlu specify size. Hanya bile add item dlm list tue je bru la system akn specifykan 1 memory address. Lagi efficient kan?

Contoh laen adalah penggunaan "using" statement dlm Object declaration.Aku bgi contoh code kat bwah nie:

Contoh 1:

void ReadFile()
{
StreamReader ObjReader = new StreamReader(@/"daunkelapasawit.txt");
String temp = ObjReader.Readline();
}

Contoh 2:

void ReadFile()
{
using ( StreamReader ObjReader = new StreamReader(@/"daunkelapasawit.txt"))
{
String temp = ObjReader.Readline();
}
}

Sori ar indentation xbtul, almaklum la write trus kat blog jer, mane ader intelliSense. So ape beza kedua2 contoh kat atas nie? Contoh 1 ada usual object declaration y bese kite buat. Bile kite exit function ReadFile, memory address utk Object tue actually still exist sebenanye. Makin byk bnde2 cmni, mkin berat la application kite. One of the solution kalu dlm C# adalah melalui penggunaan "using" statement. Maksudnye, systeam (or .Net Framework) akn detect bile Object tue dh x digunakan lgi (or out of the function scope), die akn automatically dispose Object tersebut.Dgn kate laen, bile dh xgune, buang jer la..Wat beratkan beg jer nk angkut ke sana ke mari.

Byk lagi cra2 utk makesure program kite running dengan smooth n steady. Nie antara cara2 y bleh kite guna selaku programmer. Ape pun, explore, try n gain knowledge by yourself is better than relying on someone..Gud luck!







.

4 comments:

  1. Aku dh agak dah

    kalu post sal bnde2 ilmiah nie mesti xda sapa2 nk respon..kan2?? tul x ckap aku dlu??

    ReplyDelete
  2. wah..yg ni br aku terperasan..
    ritu dr noraini ada mention psl mende ni dlm kelas parallel..

    cube tunjuk sket zid teknik guna using tu..
    n letak la skali beza pd penggunaan memory..
    nk tgk yg simple jek...juz to have a better view..

    ReplyDelete
  3. oi. aku komen ni. sbb baru baca kay..gune using itu sesuatu yang beru bg ak! yay! mekacih2

    ReplyDelete

terima kasih.