Author Topic: suggest adding -O2 to makeline  (Read 3259 times)

0 Members and 1 Guest are viewing this topic.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
suggest adding -O2 to makeline
« on: December 25, 2018, 12:08:21 am »
in the directory qb64/internal/c are three makeline text files, makeline_win.txt, makeline_lnx.txt and makeline_osx.txt
I suggest adding -O2 after g++, that way your programs will be compiled with optimization
-O2 is a safe near optimal setting in my opinion and your programs will run much faster.
as an example here's the nbody program I translated to QB64
time before adding -O2 to makeline_osx.txt 138.84375 seconds, after adding -O2, 42.6875 seconds, that's 3.25 times faster
Code: QB64: [Select]
  1. ' https://go.googlesource.com/exp/+/master/shootout/nbody.c
  2. ' The Computer Language Benchmarks Game
  3. ' https://benchmarksgame-team.pages.debian.net/benchmarksgame/
  4.  
  5. ' contributed by Christoph Bauer
  6. '
  7. ' translated to QB64 by Jack
  8. ' modified a bit to avoid using pointer to array
  9.  
  10.  
  11. PRINT STR$(main(50000000)) + " seconds"
  12.  
  13. CONST pi = 3.141592653589793
  14. CONST solar_mass = (4 * pi) * pi
  15. CONST days_per_year = 365.24
  16.  
  17. TYPE planet
  18.     x AS DOUBLE
  19.     y AS DOUBLE
  20.     z AS DOUBLE
  21.     vx AS DOUBLE
  22.     vy AS DOUBLE
  23.     vz AS DOUBLE
  24.     mass AS DOUBLE
  25.  
  26. SUB advance (nbodies AS LONG, bodies() AS planet, dt AS DOUBLE)
  27.     DIM i AS LONG, j AS LONG
  28.     DIM dx AS DOUBLE, dy AS DOUBLE, dz AS DOUBLE, distance AS DOUBLE, mag AS DOUBLE
  29.     FOR i = 0 TO nbodies - 1
  30.         FOR j = i + 1 TO nbodies - 1
  31.             dx = bodies(i).x - bodies(j).x
  32.             dy = bodies(i).y - bodies(j).y
  33.             dz = bodies(i).z - bodies(j).z
  34.             distance = SQR(((dx * dx) + (dy * dy)) + (dz * dz))
  35.             mag = dt / ((distance * distance) * distance)
  36.             bodies(i).vx = bodies(i).vx - (dx * bodies(j).mass) * mag
  37.             bodies(i).vy = bodies(i).vy - (dy * bodies(j).mass) * mag
  38.             bodies(i).vz = bodies(i).vz - (dz * bodies(j).mass) * mag
  39.             bodies(j).vx = bodies(j).vx + (dx * bodies(i).mass) * mag
  40.             bodies(j).vy = bodies(j).vy + (dy * bodies(i).mass) * mag
  41.             bodies(j).vz = bodies(j).vz + (dz * bodies(i).mass) * mag
  42.         NEXT
  43.     NEXT
  44.     FOR i = 0 TO nbodies - 1
  45.         bodies(i).x = bodies(i).x + dt * bodies(i).vx
  46.         bodies(i).y = bodies(i).y + dt * bodies(i).vy
  47.         bodies(i).z = bodies(i).z + dt * bodies(i).vz
  48.     NEXT
  49.  
  50. FUNCTION energy# (nbodies AS LONG, bodies() AS planet)
  51.     DIM i AS LONG, j AS LONG
  52.     DIM e AS DOUBLE, dx AS DOUBLE, dy AS DOUBLE, dz AS DOUBLE, distance AS DOUBLE, mag AS DOUBLE
  53.     e = 0.0
  54.     FOR i = 0 TO nbodies - 1
  55.         e = e + (0.5 * bodies(i).mass) * (((bodies(i).vx * bodies(i).vx) + (bodies(i).vy * bodies(i).vy)) + (bodies(i).vz * bodies(i).vz))
  56.         FOR j = i + 1 TO nbodies - 1
  57.             dx = bodies(i).x - bodies(j).x
  58.             dy = bodies(i).y - bodies(j).y
  59.             dz = bodies(i).z - bodies(j).z
  60.             distance = SQR(((dx * dx) + (dy * dy)) + (dz * dz))
  61.             e = e - (bodies(i).mass * bodies(j).mass) / distance
  62.         NEXT
  63.     NEXT
  64.     energy# = e
  65.  
  66. SUB offset_momentum (nbody AS LONG, bodies() AS planet)
  67.     DIM px AS DOUBLE, py AS DOUBLE, pz AS DOUBLE
  68.     DIM I AS LONG
  69.     FOR I = 0 TO nbody - 1
  70.         px = px + bodies(I).vx * bodies(I).mass
  71.         py = py + bodies(I).vy * bodies(I).mass
  72.         pz = pz + bodies(I).vz * bodies(I).mass
  73.     NEXT
  74.     bodies(0).vx = (-px) / ((4 * pi) * pi)
  75.     bodies(0).vy = (-py) / ((4 * pi) * pi)
  76.     bodies(0).vz = (-pz) / ((4 * pi) * pi)
  77.  
  78.  
  79. FUNCTION main# (n AS LONG)
  80.     CONST NBODIES = 5
  81.     DIM bodies(0 TO 4) AS planet
  82.     bodies(0).x = 0: bodies(0).y = 0: bodies(0).z = 0: bodies(0).vx = 0: bodies(0).vy = 0: bodies(0).vz = 0: bodies(0).mass = (4 * pi) * pi
  83.     bodies(1).x = 4.84143144246472090D+00: bodies(1).y = -1.16032004402742839D+00: bodies(1).z = -1.03622044471123109D-01: bodies(1).vx = 1.66007664274403694D-03 * days_per_year: bodies(1).vy = 7.69901118419740425D-03 * days_per_year: bodies(1).vz = (-6.90460016972063023D-05) * days_per_year: bodies(1).mass = 9.54791938424326609D-04 * ((4 * pi) * pi)
  84.     bodies(2).x = 8.34336671824457987D+00: bodies(2).y = 4.12479856412430479D+00: bodies(2).z = -4.03523417114321381D-01: bodies(2).vx = (-2.76742510726862411D-03) * days_per_year: bodies(2).vy = 4.99852801234917238D-03 * days_per_year: bodies(2).vz = 2.30417297573763929D-05 * days_per_year: bodies(2).mass = 2.85885980666130812D-04 * ((4 * pi) * pi)
  85.     bodies(3).x = 1.28943695621391310D+01: bodies(3).y = -1.51111514016986312D+01: bodies(3).z = -2.23307578892655734D-01: bodies(3).vx = 2.96460137564761618D-03 * days_per_year: bodies(3).vy = 2.37847173959480950D-03 * days_per_year: bodies(3).vz = (-2.96589568540237556D-05) * days_per_year: bodies(3).mass = 4.36624404335156298D-05 * ((4 * pi) * pi)
  86.     bodies(4).x = 1.53796971148509165D+01: bodies(4).y = -2.59193146099879641D+01: bodies(4).z = 1.79258772950371181D-01: bodies(4).vx = 2.68067772490389322D-03 * days_per_year: bodies(4).vy = 1.62824170038242295D-03 * days_per_year: bodies(4).vz = (-9.51592254519715870D-05) * days_per_year: bodies(4).mass = 5.15138902046611451D-05 * ((4 * pi) * pi)
  87.  
  88.     DIM i AS LONG
  89.     DIM t AS DOUBLE
  90.     t = TIMER
  91.     CALL offset_momentum(NBODIES, bodies())
  92.     PRINT USING "##.#########"; energy#(NBODIES, bodies())
  93.     FOR i = 1 TO n
  94.         CALL advance(NBODIES, bodies(), 0.01)
  95.     NEXT
  96.     PRINT USING "##.#########"; energy#(NBODIES, bodies())
  97.     main# = TIMER - t
  98.  
« Last Edit: December 25, 2018, 12:50:50 am by jack »