Speed of FreeBasic

For other topics related to the FreeBASIC project or its community.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: Speed of FreeBasic

Postby D.J.Peters » Mar 22, 2018 6:05

Only for fun here are how it looks.

Joshy

Code: Select all

Const as double pi = 3.141592653589793
Const as double solar_mass = 4*pi*pi
Const as double DPY = 365.24 ' days per years

Type planet
  as double x,y,z,vx,vy,vz,mass
End Type

Dim Shared as PLANET bodies(...) = { _
  ( 0, 0, 0, 0, 0, 0, solar_mass), _
  ( 4.84143144246472090e+00      ,-1.16032004402742839e+00      , -1.03622044471123109e-01, _
    1.66007664274403694e-03 * DPY, 7.69901118419740425e-03 * DPY, -6.90460016972063023e-05 * DPY, _
    9.54791938424326609e-04 * solar_mass), _
  ( 8.34336671824457987e+00      , 4.12479856412430479e+00      , -4.03523417114321381e-01,_
   -2.76742510726862411e-03 * DPY, 4.99852801234917238e-03 * DPY,  2.30417297573763929e-05 * DPY, _
    2.85885980666130812e-04 * solar_mass), _
  ( 1.28943695621391310e+01      ,-1.51111514016986312e+01      , -2.23307578892655734e-01, _
    2.96460137564761618e-03 * DPY, 2.37847173959480950e-03 * DPY, -2.96589568540237556e-05 * DPY, _
    4.36624404335156298e-05 * solar_mass),  _
  ( 1.53796971148509165e+01      ,-2.59193146099879641e+01      ,  1.79258772950371181e-01, _
    2.68067772490389322e-03 * DPY, 1.62824170038242295e-03 * DPY, -9.51592254519715870e-05 * DPY, _
    5.15138902046611451e-05 * solar_mass)}

Sub advance(Byval dt As Double)
  dim as double dx,dy,dz,distance,mag,aMass,bMass
  For i as integer = 0 To 4
    var a = @bodies(i)
    For j  as integer = i + 1 To 4
      var b = @bodies(j)
      dx=a->x-b->x : dy=a->y-b->y : dz=a->z-b->z
      distance = Sqr(dx*dx + dy*dy + dz*dz)
      mag = dt / (distance * distance * distance)
      aMass=a->mass*mag : bMass=b->mass*mag
      a->vx -= dx*bMass : a->vy -= dy*bMass : a->vz -= dz*bMass
      b->vx += dx*aMass : b->vy += dy*aMass : b->vz += dz*aMass
    Next
  Next
  For i as integer = 0 To 4
    with bodies(i)
      .x += dt*.vx : .y += dt*.vy : .z += dt*.vz
    end with 
  Next
End Sub

sub drawbodies()
  dim as integer w,h
  screeninfo w,h:w*=0.5:h*=0.5
  for i as integer=0 to 4
    var z=bodies(i).z+10
    if z>0 then
      circle(w+(bodies(i).x*170)/z,h+(bodies(i).y*170)/z),bodies(i).mass*iif(i=0,2,1000),iif(i=0,14,i),,,,F
    end if
  next 
end sub
'
' main
'
dim as integer w,h
screeninfo w,h:w*=0.9:h*=0.9
screenres w,h,,2
screenset 1,0
'offset_momentum
while inkey=""
  cls
  drawbodies
  advance(0.01)
  flip
  sleep 10
wend
beauty

Code: Select all

#include once "crt/math.bi"

CONST NBODIES = 500

Type planet
  as single x,z,vx,vz,mass
End Type

dim shared as planet ptr ptr bodies
sub initBody(b as planet ptr)
  dim as single s,r=100+rnd*20,w=6.28*rnd,l
  b->x=cos(w)*r
  b->z=sin(w)*r
  b->vx=cos(w+0.2)*r - b->x
  b->vz=sin(w+0.2)*r - b->z
  l=sqrtf(b->vx*b->vx + b->vz*b->vz)
  s=50+rnd*50
  b->vx/=l:b->vx*=s
  b->vz/=l:b->vz*=s
  b->mass=10+rnd*10
end sub

sub initBodies
  dim as single r,w
  bodies = callocate(sizeof(PLANET ptr)*NBODIES)
  for i as integer = 0 to NBODIES-1
    bodies[i]=callocate(sizeof(PLANET))
    initBody(bodies[i])   
  next
  bodies[0]->x=0
  bodies[0]->z=0
  bodies[0]->vx=0
  bodies[0]->vz=0
  bodies[0]->mass=NBODIES*1000
 
end sub

Sub advance(Byval dt As Double)
  dim as single dx,dz,dsquared,distance,mag,aMass,bMass
  For i as integer = 0 To NBODIES-2
    var a = bodies[i]
    For j  as integer = i + 1 To NBODIES-1
      var b = bodies[j]
      dx=a->x-b->x : dz=a->z-b->z
      dsquared=dx*dx + dz*dz
      if dsquared then
        distance = sqrtf(dsquared)
        mag = dt / (distance * distance * distance)
        aMass=a->mass*mag : bMass=b->mass*mag
        a->vx -= dx*bMass : a->vz -= dz*bMass
        b->vx += dx*aMass : b->vz += dz*aMass
      end if 
    Next
  Next
  For i as integer = 1 To NBODIES-1
    var b = bodies[i]
    b->x += dt*b->vx : b->z += dt*b->vz
    'b->vx*=0.99 : b->vz*=0.99
  Next
End Sub

sub drawbodies()
  dim as integer w,h,w2,h2,x,y
  dim as ulong c
  screeninfo w,h
  w2=w\2:h2=h\2
  dim as ubyte ptr r,p=screenptr()
  for y=0 to h-1
    for x=0 to w-1
      if *p>4 then *p-=4
      p+=1
    next
  next 
  p=screenptr() 
 
  var b = bodies[0]
  circle(w2+b->x,h2+b->z),10,255
  for i as integer=1 to NBODIES-1
    b = bodies[i]
    x=w2+b->x : y=h2+b->z
    if x>-1 andalso x<w andalso y>-1 andalso y<h then
      p[y*w+x]=255
    end if 
    if x<-100000 or x>100000 then initBody(b):continue for
    if y<-100000 or y>100000 then initBody(b):continue for
  next 
end sub
'
' main
'
dim as integer w,h
initbodies
screeninfo w,h:w*=0.9:h*=0.9
screenres 640,480,,2
screenset 1,0
for i as integer=0 to 255
  palette i,i,i,i
next
while inkey=""
  drawbodies
  advance(0.01)
  flip
  'sleep 10
wend

deltarho[1859]
Posts: 1765
Joined: Jan 02, 2017 0:34
Location: UK

Re: Speed of FreeBasic

Postby deltarho[1859] » Mar 22, 2018 14:10

jj2007 wrote:Engineers and mathematicians often disagree ;-)

Quite so. <smile>

Have a think about this.

If we have 21 timings and sort them to give test(1 to 21) then for any test(i), where i = 1 to 10, changing it will have no effect on test(11) provided that test(i) < test(11). TeeEmCee mentioned throttling as a possible reason for the early tests being faster. Well, I have a high performance liquid cooler on board so I not sure about that but even if that is the case we could have one, two or three particularly fast results but provided that they are less than test(11) then test(11) will still hold. At the other end of the scale if some results are particularly slow because of system activity then, by the same token, provided that they exceed test(11) then, again, test(11) will still hold.

In both of the above scenarios the mean will vary. That is, the mean is sensitive to both 'rogue' fast and slow timings. You will condition the results in an attempt to mitigate that by forcing symmetry so as to apply a mean but in my case, with a median, I need do nothing. Another test with 21 timings may see more or less 'fast' timings and may see more or less 'slow' timings but the median is likely to be pretty much the same. It is anybody's guess what the mean would be.
srvaldez
Posts: 1955
Joined: Sep 25, 2005 21:54

Re: Speed of FreeBasic

Postby srvaldez » Mar 22, 2018 14:57

nice examples Joshy :-)
zxretrosoft
Posts: 21
Joined: Apr 23, 2013 19:12
Contact:

Re: Speed of FreeBasic

Postby zxretrosoft » Mar 23, 2018 7:24

Thank you all!

just out of curiosity ...
Is there any possibility of intentionally reducing the speed of FB?

E.g. I want it to work as if I had a 3.5 MHz CPU (ZX Spectrum).
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: Speed of FreeBasic

Postby D.J.Peters » Mar 23, 2018 8:41

zxretrosoft wrote:Is there any possibility of intentionally reducing the speed of FB?
Not really of course you can use sleep() inside of loops but that isn't what you would accept.

You can use many ":" between code and commands
than you can use in your IDE replace ": " with ": sleep 10 :"

Code: Select all

for i=1 to 10
  a*=2 : a+= s/5 : result = i*a*a :
  :
next
becomes:

Code: Select all

for i=1 to 10
  a*=2 : sleep 10  : a+= s/5 : sleep 10  : result = i*a*a : sleep 10  :
  : sleep 10  :
next
to restore it replace ": sleep 10 : " with ":"

Joshy
zxretrosoft
Posts: 21
Joined: Apr 23, 2013 19:12
Contact:

Re: Speed of FreeBasic

Postby zxretrosoft » Mar 23, 2018 16:57

Thanks Joshy! Good idea :-)
deltarho[1859]
Posts: 1765
Joined: Jan 02, 2017 0:34
Location: UK

Re: Speed of FreeBasic

Postby deltarho[1859] » Mar 23, 2018 17:36

You could use a timer and periodically 'swan off' for a nap/Sleep. That way you would have 'wall to wall' slowing down.

Don't forget that a value of Sleep less than the system clock of 15.625ms ( 1000/64Hz ) will actually sleep for 15.625ms. Link Sleep to a millisecond timer and you can then sleep for 1, 2, 3 and so on milliseconds with a resolution of one millisecond.
marcov
Posts: 2751
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: Speed of FreeBasic

Postby marcov » Apr 13, 2018 7:57

jj2007 wrote: When timing this code, you use only a tiny subset of the functions of a language. It is not at all representative of the tasks that a programming language has to perform in real life applications.


And the ground rule is that the form of the benchmark is a model for the speed of the application.

So a one (source) file math problem is a good benchmark for applications that have solving a math problem in one single (source) file as rate determining step (even though the app can be larger, e.g. GUI screens).

Similarly if the benchmarks rds is also in one single function, that also goes for its application.

But a single function math benchmark is not a benchmark for an application that zips from functions in a library, to several application source files with complicated call chains. The optimizer will not be able to make the same optimizations as in the single function math benchmark.

Many Unix C compilers have certain optimizations because they optimize certain core loops of interpreters (and then any improvement helps because it is directly noticeable in the interpreted language execution speed). While they are sometimes useful in short one source file proograms too, they usually don't lead to much improvement in normal applications.
Boris the Old
Posts: 132
Joined: Feb 04, 2011 20:34
Location: Ontario, Canada

Re: Speed of FreeBasic

Postby Boris the Old » Apr 18, 2018 3:21

Trying to optimize a program is like hunting for unicorns -- it seems like a good idea but the results are always disappointing.

There's no significant difference in programming languages, since all code ultimately exists only as machine code. And since most applications spend 90% or more of their time executing libraries in multitasking operating systems, there's little to be gained from counting machine cycles.

Our energies are best spent writing maintainable, and bug free, code that produces the results that the user wants, and to do it in a way that makes the best use of the language's features.

Rod
jj2007
Posts: 1161
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Speed of FreeBasic

Postby jj2007 » Apr 18, 2018 6:59

Boris the Old wrote:Trying to optimize a program is like hunting for unicorns -- it seems like a good idea but the results are always disappointing.
Are you working for KingSoft or LibreOffice?
The KingSoft suite looks nice indeed. It is a bit slow, though; I made some tests with a 20MB tab-delimited file, loading is a factor 2 slower than with M$ Excel, sorting even a factor 5 or so (and I remember that OpenOffice was even slower). My unfinished spreadsheet control is another factor 3-5 faster

Seriously, there is a lot of really, really slow software around. M$ Visual Crap Community, for example, is a joke. Everything built with the QT "framework" is ridiculously bloated and slow. The list is long...

My modest Core i5 runs at 2,500 MHz, that's a factor 5 faster than the fastest processor available 20 years ago. And my machine is definitely much slower than the one I had 20 years ago. The reason? Coders and their managers who think that optimising is not worth the effort.
lizard
Posts: 440
Joined: Oct 17, 2017 11:35
Location: Germany

Re: Speed of FreeBasic

Postby lizard » Apr 18, 2018 10:27

jj2007 wrote:
Boris the Old wrote:Trying to optimize a program is like hunting for unicorns -- it seems like a good idea but the results are always disappointing.
Are you working for KingSoft or LibreOffice?

We are always searching for the "ideal" program but never really find it. I am still searching for perfect filemanager, IDE, and others but maybe better not to find them because then the search is over.

Seriously, there is a lot of really, really slow software around. M$ Visual Crap Community, for example, is a joke. Everything built with the QT "framework" is ridiculously bloated and slow. The list is long...

Many people are thinking, the bigger filesize, the better it must be. And i must confess, i myself have been sometimes thinking that way. Only few experts like here in forum understand, that programs are better with smaller filesize. Same goes for many things. The bigger the better. We can't change the world.


My modest Core i5 runs at 2,500 MHz, that's a factor 5 faster than the fastest processor available 20 years ago. And my machine is definitely much slower than the one I had 20 years ago. The reason? Coders and their managers who think that optimising is not worth the effort.

Glad i am not the only one thinking that way. I remember old days with 286 and 486, they were sometimes as fast or even faster than the boxes today. Considering the processors are much faster today, the OSes should be much faster. Seems the professional programmers are not much interested to speed up their programs, like you have written. They are more interested to earn money.
MrSwiss
Posts: 3083
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Speed of FreeBasic

Postby MrSwiss » Apr 18, 2018 11:19

Well, it appears to be based generally on two, different *frames of mind*.

1) making lots of money (without caring for quality: speed/size/bug-free code):
This is usually achieved by: using huge libraries [mono/.net/gtk] which themselves
are not likely, to be bug-free, but surely add plenty overhead.

2) making top quality software (largely without caring, about the money angle):
This is usually achieved by: writing every routine oneself (optimized for: speed/
size, and tested [bug avoidance]).

I've on purpose *overdone* it above, by stating the extremes. Aware of the fact,
that most of the times, the truth is somewhere inbetween the two extremes.
ur_naz
Posts: 46
Joined: Mar 02, 2016 12:44

Re: Speed of FreeBasic

Postby ur_naz » Apr 29, 2018 23:05

free pascal is better then free basic in akkerman function test.
Final standings is:
1. free pascal (3,14)
2. free basic, c++, sbcl(3,12)
3. c# (3,11)
4. others
Last edited by ur_naz on Apr 29, 2018 23:18, edited 1 time in total.
jj2007
Posts: 1161
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Speed of FreeBasic

Postby jj2007 » Apr 29, 2018 23:14

ur_naz wrote:free pascal is better then free basic in akkerman function test.

Brilliant! Can you post your test code please? Or at least a link to the tests?
ur_naz
Posts: 46
Joined: Mar 02, 2016 12:44

Re: Speed of FreeBasic

Postby ur_naz » Apr 29, 2018 23:23

they ar all very easy to write and similar to each other
this is fb

Code: Select all

DECLARE Function Akkerman (as ULong, as ULong) as ULong


Const a = 3, b = 12

Print "Akkerman function FreeBasic compiler test"

Print "Please wait while calculating..."

Print "for m="; a; " & n="; b ; " F = "; Akkerman(a, b)

Sleep

end

Function Akkerman (m as ULong, n as ULong) as ULong

   If m=0 Then
        return n + 1
     ElseIf n = 0 then
        return Akkerman ( m-1, 1)
     Else
        return Akkerman ( m-1, Akkerman ( m, n-1)) 

End If

End Function

this is pascal

Code: Select all

program AkkermanTest;

const
  m = 3;
  n = 14;

  function Akkerman( m, n : LongWord ) : LongWord;

  begin
   if m = 0 then

      Akkerman := n + 1

    else
      if n = 0 then

        Akkerman := Akkerman(m - 1 , 1)

        else
       begin

          Akkerman := Akkerman( m - 1, Akkerman(m, n - 1));

       end;
  end;

begin

  writeln('Akkerman function FreePascal/Lazarus compiler test');
  writeln('Please wait while calculating...');
  writeln('for m=', m, ' & n=', n , ' F = ', Akkerman(m,n));

  readln;

end.

and so on

Return to “Community Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests