Author: Graham Laight
Date: 07:00:51 01/03/03
Go up one level in this thread
On January 03, 2003 at 07:52:46, Graham Laight wrote: >On January 01, 2003 at 11:50:58, Lieven Clarisse wrote: > >>I was wondering if there is a good book about how to write efficient C code. I >>am not talking about algorithms, but the way *how* to write things, eg >> >>which is faster : >>do {} while() or while () {} >> >>------- >>I know for instance that: >> >>ptr=&R[i]; >>if((*ptr==3)||(*ptr==7)) {;} >> >>is faster then: >> >>if((R[i]==3)||(R[i]==7)) {;} >> >>Is there a good book that reviews all those kinds of tricks? >> >>regards, >> >>Lieven > >Forgetting the pointer and saving to a variable DOES save time - and now I can >prove it! > >Below is a web page I have written, with the button actions written in >JavaScript. > >The "easy to maintain" button does a million loops using the > >if((R[1]==3)||(R[1]==7)) > >construct, wheras the "Potentially Quicker" button does a million loops using >the > > x = R[1]; > if((x==3)||(x==7)) > >construct. On my computer, with IE 5.5, it takes 2.013 seconds the easy way, but >only 1.883 seconds the quick way. On further testing, I found that the times to execute the code can vary significantly (presumably depending on what else my computer is doing). I have therefore tested again several times, and just taken the fastest time for each button. New Results =========== Easy to maintain function: 1.992 seconds Potentially quicker function: 1.873 seconds This is a difference of 119 milliseconds, or about 6% Also note that because this is true in JavaScript doesn't necessarily make it true in C - I'll leave that to someone with a C compiler to test. -g >If you want to prove this for yourself: > >1. copy the web page below > >2. paste it into Notepad > >3. save it to your c:\ drive as Graham_Demo.htm > >4. point your web browser at it (I wrote and tested under IE 5.5 - should also >work under Netscape or Mozilla) > >-g n.b. if you copy the web page below now, you should go to the parent message to copy it - otherwise you'll have to remove the > from the start of each line. ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> > ><html> ><head> ><title>Graham's Optimisation Demo</title> ></head> ><body> ><SCRIPT LANGUAGE="JavaScript"> >function easyToMaintain() >{ > var R = new Array(); > R[0] = 1; > R[1] = 2; > R[2] = 3; > > var p = 0; > var startTime = new Date(); > > for (k = 0; k < 1000000; k++) > { > if((R[1]==3)||(R[1]==7)) > p = p + 1; > } > > var endTime = new Date(); > var totalTime = endTime.getTime() - startTime.getTime(); > alert("The easy to maintain version took " + totalTime + " milliseconds to >run"); >} > >function possiblyQuicker() >{ > var R = new Array(); > R[0] = 1; > R[1] = 2; > R[2] = 3; > > var p = 0; > var x = 0; > var startTime = new Date(); > > for (k = 0; k < 1000000; k++) > { > x = R[1]; > if((x==3)||(x==7)) > p = p + 1; > } > > var endTime = new Date(); > var totalTime = endTime.getTime() - startTime.getTime(); > alert("The potentially quicker version took " + totalTime + " milliseconds to >run"); >} > ></SCRIPT> ><input type="button" value="Test Easy-To-Maintain Version Of Code" >onclick="easyToMaintain()"> ><input type="button" value="Test Possibly-Quicker Version Of Code" >onclick="possiblyQuicker()"> ></body> ></html>
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.