Writing Fast Programs: A Practical Guide for Scientists and Engineers

Appendix E: Basic Listings for the Part I and Part II Demo Programs

Listing 1.1: Pure Scientist Style LJ Energy Computation in BASIC
E=0For i=1 to N  For j=1 to N    'avoid division by zero when i=j    If i<>j Then      E=E+((? /sqr((x(i)-x(j))^2+       (y(i)-y(j))^2)^6 + ? /sqr((x(i)-x(j))^2       +(y(i)-y(j))^2)^12    End If  Next jNext iE=E*-C'avoids double counting the energies since i'and j are looped over all NE=E/2
Listing 1.2: Short source code for array assignment
'SomeValue is defined previously in codeFor Count=1 to 10     R(Count)=SomeValue*CountNext Count
Listing 1.3: Efficient, though longer, source code
'SomeValue is defined previously in codeR(1)=SomeValue*1R(2)=SomeValue*2R(3)=SomeValue*3R(4)=SomeValue*4R(5)=SomeValue*5R(6)=SomeValue*6R(7)=SomeValue*7R(8)=SomeValue*8R(9)=SomeValue*9R(10)=SomeValue*10
Listing 1.4: Using symmetry to improve Listing 1.1
For I=1 to N-1     For J=I+1 to N          {code to calculate pairwise energy}     Next JNext I
Listing 5.3: Floating point arguments with a LONG loop counter
'conversion factor for'degrees to radians,'divided by 100Const cDeg2Rad as DOUBLE = ? /18000Dim Counter as LONG' holder for the computed sinDim dummy as DOUBLE' LONG cannot step 0.01,' so actual 'degree' * 100For Counter = 0 to 36000 Step 1     ' Counter must be converted to     ' double precision radians     dummy = sin(CDbl(Counter) * cDeg2Rad)Next Counter
Listing 5.4: Floating-point arguments with floating point loop counter
' degrees to radiansConst Deg2Rad as DOUBLE...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Logic Counters
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.