Speed Speed Speed: FB compiler benchmarks

General FreeBASIC programming questions.
jj2007
Posts: 1214
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Speed Speed Speed: FB compiler benchmarks

Postby jj2007 » Mar 07, 2019 11:21

Below a little testbed to benchmark our three main compilers: Gas, Gcc32 and Gcc64. It consists of two parts:

1. the main testbed, set up for little routines, for example (load mshtmlc.bi into a string array, then check how many lines contain the word "define" using the bmParser() function):

Code: Select all

  total=0
  Print Chr(10);"testing bmParser:"   ' ########## test ##############
  t=timer      ' reset timer for this benchmark
  for ct=1 to loops/2
   total+=bmParser(MyString(), "define")
  next
  Print using "##.### seconds" ; timer - t;
  Print " for";loops*2;"*find a string"

2. an include file LoadSaveFile.bi to load text files into string arrays using Recall("somefile.txt", someStringArray()). In the test below, I picked as inputfile "mshtmlc.bi", a text file with about 2.4MB that should be in your path.

If you have good ideas for testing relevant features of the language (arithmetic, sorting, whatever), please post them here, and I'll try to update the testbed accordingly. If I understand correctly, FB has no built-in sort commands, right?

Output example:

Code: Select all

*** This is a FB 1.05.0 32-bit executable compiled with gas running on Windows ***
Start of tests

testing Recall:
 0.589 seconds for 50*Recall

testing bmParser:
 0.535 seconds for 200*find a string



***************************************************************************************************************************
1. *** main testbed ***

Code: Select all

' *** compiler benchmark: parsing, arithmetic, strings, sorting, ... ***
' print "*** tmplate for benchmarking Gas, Gcc & friends ***"
#define loops 100   ' use 100 for getting around 0.5 ... 1 second per test
' #include "Windows.bi"   ' for e.g. MessageBox(0, "Hello string", "Hello title", MB_OK)
#Include "LoadSaveFile.bi"   ' LoadFile, SaveFile, Recall, Cell
#define elements 10000   ' for arrays etc; note that adding one 0 makes Gcc64 choke
#define testfile 1   ' adjust to your local availability ;-)
#if testfile=0
   #define inputfile "MacStrings.txt"   ' CR only
#elseif testfile=1
   #define inputfile "mshtmlc.bi"   ' Unix format, LF only
#elseif testfile=2
   #define inputfile "\Masm32\MasmBasic\Res\SkelMbEmpty.lst"
#elseif testfile=3
   #define inputfile "LoadSaveFile.bi"   ' Windows, CrLf
#else
   #define inputfile "\Masm32\MasmBasic\Res\DualWin.inc"
#endif

' *********** give feedback on the compiler **********
Print "*** This is a FB " & __FB_VERSION__;
#ifdef __FB_64BIT__
   print " 64-bit executable compiled with ";
#else
   print " 32-bit executable compiled with ";
#endif
print __FB_BACKEND__; " running ";
#ifdef __Fb_Win32__
   print "on Windows ***"
#else
  #ifdef __FB_LINUX__
   print "on Linux ***"
  #else
   print "neither on Windows nor on Linux ***"
  #endif
#endif

print "Dim two double arrays with";elements;" elements"
Dim As Double dubA(elements), dubB(elements)
#if 0
Sub modify( c() As Double, d() As Double)
    For i As Integer = 1 To elements
        d(i) = c(elements - i + 1)
    Next i
End Sub

print "Trying modify:"
modify( dubA(), dubB() )

For i As Integer = 1 To Min(2, elements)
    Print i, dubA(i), dubB(i)
Next i
#endif

function bmParser(txt() as string, findme as string) as integer
  Dim as integer sum
  For i As Integer = 0 To UBound(txt)-1
   if Instr(txt(i), findme) Then sum+=1
  Next
  function=sum
end function
sub bmSingle(sing() as single)
  Dim as single sum
  For i As Integer = 1 To elements
   if sing(i)<100.0 Then sum+=sing(i)
  Next
end sub
sub bmDouble(dub() as double)
end sub

Dim MyInt As integer=123456
Dim MySingle As Single=123.456
Dim MyDouble As Double=123.456


' ************************** start of tests **************************
  Print "Start of tests"

  Dim MyString() As String
  Dim as long ct, records, total
  Dim as double t   ' the timer

  Print Chr(10);"testing Recall:"   ' ########## test ##############
  t=timer      ' reset timer for this benchmark
  for ct=1 to loops/2
   records=Recall(inputfile, MyString())
  next
  Print using "##.### seconds" ; timer - t;
  Print " for";loops/2;"*Recall"

  total=0
  Print Chr(10);"testing bmParser:"   ' ########## test ##############
  t=timer      ' reset timer for this benchmark
  for ct=1 to loops/2
   total+=bmParser(MyString(), "define")
  next
  Print using "##.### seconds" ; timer - t;
  Print " for";loops*2;"*find a string"

  If records>=0 Then
   ' *** verify that the formats are being correctly recognised ***
   Print Chr(10);UBound(MyString); " strings found in "; inputfile
   For ct as integer=0 to 9
      ' print ct, "[";Cell(ct, 2, MyString());"]"
      print ct, "[";MyString(ct);"]"
   Next
  else
   print "There was an error"
  endif
  sleep


***************************************************************************************************************************
2. *** include file, save as LoadSaveFile.bi ***

Code: Select all

' *** 7 March 2019, jj2007 for the FreeBasic forum - freeware, use at your own risk ***
#include once "crt.bi"   ' needed for memcpy

Function Recall(fname As String, locArray() As String) As Integer
  Dim As Integer ct=0, c13off=0, cursize=1000
  Dim As Integer f=Freefile, sBytes
  Dim As ubyte c1310
  If Open(fname For Binary Access Read As #f) Then
   return -1      ' some error
  else
   Dim as ubyte ptr pContent = Allocate(Lof(f)), CurPos=pContent, CrPos
   Dim as string delimiter=Chr(10)
   Dim as long flen=Lof(f)
   Get #f, 1, *pContent, Lof(f)
   Close #f
   Do
       c1310=pContent[Ct]
       Ct+=1
   Loop until c1310<=13 and c1310<>9
   #if 0
       13   1   Windows
       13   0   Mac
       10   0   Unix
   #endif
   if pContent[Ct]=10 Then c13off=1
   if c1310=13 and pContent[Ct]<>10 Then delimiter=Chr(13)
   if c1310=10 and pContent[Ct]=13 Then delimiter=Chr(10) : c13off=1   ' exotic case of malformed *.lst file
   ' print "C1310=";c1310;", c13off=";c13off
   Ct=0
   Do
       if ct=0 or ct>cursize then
          cursize+=cursize shr 1
          ReDim Preserve locArray(cursize)
       endif
       CrPos=strstr(CurPos, delimiter)
       if CrPos=0 Then CrPos=pContent+flen:flen=0
       sBytes=CrPos-CurPos-c13off
       if sBytes>0 then
          locArray(ct)=Space(sBytes)
          memcpy(StrPtr(locArray(ct)), CurPos, sBytes)
       endif
       CurPos=CrPos+1
       ct+=1
   Loop until flen=0
   While ct>1 and len(trim(locArray(ct-1)))=0   ' get rid of trailing empty strings
       ct-=1
   Wend
   ReDim Preserve locArray(ct)
   DeAllocate(pContent)
   return ct
  End If
End Function

#ifdef _WINDOWS_
Function LoadFile(file as Wstring) as String
  Dim As String i_p
  Dim As Any Ptr f
  Dim As Long fsize, bytesread
  f = CreateFileW(file, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)
  If f=INVALID_HANDLE_VALUE Then
   print "LoadFile failed"
  else
    fsize = GetFileSize(f, 0)
    i_p = String(fsize, Asc("x"))
    ReadFile(f, @i_p[0], fsize, @bytesread, 0)
    CloseHandle(f)
  End If
  Return i_p
End Function

Function SaveFile(file As Wstring, o_p As String) As Integer
  Dim As Handle n
  Dim As Long byteswritten
  n = CreateFileW(file, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0)
  If n=INVALID_HANDLE_VALUE Then
   return -1   ' SaveFile failed
  else
    WriteFile(n, @o_p[0], Len(o_p), @byteswritten, 0)
    CloseHandle(n)
   return byteswritten
  endif
End Function
#endif

#ifndef maxCell
   #Define maxCell 100   ' whatever you consider enough for a single cell
#endif

Dim shared retstr As string * maxCell+1   ' global for Cell(row, column)

Function Cell(row As integer, col As integer, locArray() As String) As string
  Dim As integer ct=0, ctTabs=0, posLeft=-1, posRight=0
  Dim As ubyte ptr pString
  Dim c As ubyte
  pString=StrPtr(locArray(row))
  if pString then
   Do
      c=pString[ct]
      if c=0 then
         if ctTabs>=col then posRight=ct+1
         if posLeft=-1 then posLeft=0
         Exit do
      endif
      if c=9 then
         ctTabs=ctTabs+1
         if col=0 then
            posLeft=0
            if ctTabs>col then
               posRight=ct+1
               Exit do
            endif
         else
            if posLeft=-1 and ctTabs>=col then
                posLeft=ct+1
            elseif ctTabs>col then
                posRight=ct+1
                Exit do
            endif
         endif
      endif
      ct=ct+1
   Loop
  endif
  if posRight=0 then
   retstr[0]=0
  else
   posRight-=posLeft
   if posRight>maxCell then posRight=maxCell
   memcpy(StrPtr(retstr), pString+posLeft, posRight)
   retstr[posRight-1]=0
  endif
  return retstr
end function

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests