Comparison QB64 compiled with gcc optimizations and without
#40
(06-10-2022, 02:41 AM)Jack Wrote: just for testing

Code: (Select All)
_Title "n-body"

'http://benchmarksgame.alioth.debian.org/u64q/program.php?test=nbody&lang=gcc&id=1
' The Computer Language Benchmarks Game
' http://benchmarksgame.alioth.debian.org/

' contributed by Christoph Bauer
'
' translated to QB64 by Jack
' modified a bit to avoid using pointer to array

declare FUNCTION main# (n AS LONG)

Print Str$(main(50000000)) + " seconds"
End

Const pi = 3.141592653589793
Const solar_mass = (4 * pi) * pi
Const days_per_year = 365.24

Type planet
    x As Double
    y As Double
    z As Double
    vx As Double
    vy As Double
    vz As Double
    mass As Double
End Type

Sub advance (nbodies As Long, bodies() As planet, dt As Double)
    Dim i As Long, j As Long
    Dim dx As Double, dy As Double, dz As Double, distance As Double, mag As Double
    For i = 0 To nbodies - 1
        For j = i + 1 To nbodies - 1
            dx = bodies(i).x - bodies(j).x
            dy = bodies(i).y - bodies(j).y
            dz = bodies(i).z - bodies(j).z
            distance = Sqr(((dx * dx) + (dy * dy)) + (dz * dz))
            mag = dt / ((distance * distance) * distance)
            bodies(i).vx = bodies(i).vx - (dx * bodies(j).mass) * mag
            bodies(i).vy = bodies(i).vy - (dy * bodies(j).mass) * mag
            bodies(i).vz = bodies(i).vz - (dz * bodies(j).mass) * mag
            bodies(j).vx = bodies(j).vx + (dx * bodies(i).mass) * mag
            bodies(j).vy = bodies(j).vy + (dy * bodies(i).mass) * mag
            bodies(j).vz = bodies(j).vz + (dz * bodies(i).mass) * mag
        Next
    Next
    For i = 0 To nbodies - 1
        bodies(i).x = bodies(i).x + dt * bodies(i).vx
        bodies(i).y = bodies(i).y + dt * bodies(i).vy
        bodies(i).z = bodies(i).z + dt * bodies(i).vz
    Next
End Sub

Function energy# (nbodies As Long, bodies() As planet)
    Dim i As Long, j As Long
    Dim e As Double, dx As Double, dy As Double, dz As Double, distance As Double, mag As Double
    e = 0.0
    For i = 0 To nbodies - 1
        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))
        For j = i + 1 To nbodies - 1
            dx = bodies(i).x - bodies(j).x
            dy = bodies(i).y - bodies(j).y
            dz = bodies(i).z - bodies(j).z
            distance = Sqr(((dx * dx) + (dy * dy)) + (dz * dz))
            e = e - (bodies(i).mass * bodies(j).mass) / distance
        Next
    Next
    energy# = e
End Function

Sub offset_momentum (nbody As Long, bodies() As planet)
    Dim px As Double, py As Double, pz As Double
    Dim I As Long
    For I = 0 To nbody - 1
        px = px + bodies(I).vx * bodies(I).mass
        py = py + bodies(I).vy * bodies(I).mass
        pz = pz + bodies(I).vz * bodies(I).mass
    Next
    bodies(0).vx = (-px) / ((4 * pi) * pi)
    bodies(0).vy = (-py) / ((4 * pi) * pi)
    bodies(0).vz = (-pz) / ((4 * pi) * pi)
End Sub


Function main# (n As Long)
    Const NBODIES = 5
    Dim bodies(0 To 4) As planet
    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
    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)
    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)
    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)
    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)

    Dim i As Long
    Dim t As Double
    t = Timer
    Call offset_momentum(NBODIES, bodies())
    Print Using "##.#########"; energy#(NBODIES, bodies())
    For i = 1 To n
        Call advance(NBODIES, bodies(), 0.01)
    Next
    Print Using "##.#########"; energy#(NBODIES, bodies())
    main# = Timer - t
End Function

hello @Jack. interesting code. this is where we see the potential of qb64 compiled with O3 optimization. thanks for sharing.

on my computer :

16.7x seconds : program compiled with qb64 -O3
66.8x seconds : program compiled with original qb64
Reply


Messages In This Thread
RE: Comparison QB64 compiled with Ofast and without - by Coolman - 06-10-2022, 09:05 AM



Users browsing this thread: 36 Guest(s)