How to reduce the filesize of a compiled file?

General FreeBASIC programming questions.
dodicat
Posts: 6493
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reduce the filesize of a compiled file?

Postby dodicat » Aug 18, 2017 8:55

You are missing some.
If line 31 is un- commented, then the result is different else the result is the same.

Code: Select all

#include "file.bi"   
#include "crt.bi"

'Run with the best - 32 bit -gen gas, the FreeBASIC as free from C as pos. (except msvcrt.dll or it's Linux counterpart)

Function loadfile(file As String) As String
   If Fileexists(file)=0 Then Print file;" not found":Sleep:End
    Dim As Long  f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(1) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

Function Tally(SomeString As String,Part As String) As Long
    Dim As zstring Ptr z=Strptr(Somestring)
    Dim As Long i=Len(Part),c
    While z
        z=iif(c,strstr(z[i],Part),strstr(z[0],Part))
         c+=1
    Wend
    Return c-1
End Function


Dim As String text=loadfile("mshtmlc.bi")
'text=lcase(text)

Dim As Double t=Timer
Print tally(text,"type")-tally(text,"end type")
Print "Time taken ";Timer-t
sleep



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

Re: How to reduce the filesize of a compiled file?

Postby jj2007 » Aug 18, 2017 11:53

Josep Roca wrote:Seconds: 0.006208108010469005
Count: 1728

Not bad for a Basic compiler.

Apparently, my file has a few more "type" than yours.

No, files are identical, and timings very similar:

Code: Select all

Seconds:       0.007457879837602377
Count:        1728

But you didn't use full word search in Instr(), so your version adds a few ones, like (line 12694 with line wrap off):
declare function IHTMLSelectionObject2_get_typeDetail_Proxy(byval This as IHTMLSelectionObject2 ptr, byval p as BSTR ptr) as HRESULT
declare sub IHTMLSelectionObject2_get_typeDetail_Stub(byval This as IRpcStubBuffer ptr, ...
How about string manipulation such appending, replacing, deleting, inserting or concatenating unicode strings? I do all my work using unicode and only use FB's ansi strings when I want to quickly allocate a byte buffer.


Most ansi string routines have a unicode equivalent, e.g. Let my$="Hello" vs wLet my$="hello". I've put an example here - no need to flood this forum with my code.
Last edited by jj2007 on Aug 18, 2017 12:47, edited 4 times in total.
dodicat
Posts: 6493
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reduce the filesize of a compiled file?

Postby dodicat » Aug 18, 2017 12:12

Yea,I agree, should look for type+one space.
Also end type and end(2 spaces) type and even end(3 spaces) type
Plus an all lowercase search.
Silly.
Results from My code:
791
Time taken 0.007318080580262176
I used my own path to the file.
"C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\inc\win\mshtmlc.bi"

My own method is probably OK for Linux also, but I cannot test that.
jj2007
Posts: 1462
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to reduce the filesize of a compiled file?

Postby jj2007 » Aug 18, 2017 12:48

791 looks a bit low, though: I find 1718. How old is your mshtmlc.bi, how many bytes (and where did you get it from)?

In my mshtmlc.bi, there are ten type occurrences that are not found when using full word Instr():

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Let esi=FileRead$("mshtmlc.bi")      ; 2,488,437 bytes
  NanoTimer()
  ; mov ecx, Count(esi, "type", 0)     ; 0=case-sensitive: 1728 times 'type' found in 0.0050 seconds
  mov ecx, Count(esi, "type", 4)       ; 4=c-s + full word: 1718 times 'type' found in 0.0043 seconds
  sub ecx, Count(esi, "end type")      ; case-sensitive (full word not necessary, it's clear enough)
  Inkey Str$("%i times 'type'", ecx), Str$(" found in %2f seconds", NanoTimer(us)/1000000)
EndOfCode

For comparison, a Replace all with "full word" and "match case" in MS WORD yields 2563-845=1718 matches.
dodicat
Posts: 6493
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reduce the filesize of a compiled file?

Postby dodicat » Aug 18, 2017 13:56

The file mshtmlc.bi is in the latest official freebasic package.
Here are details with my path to the file.

Code: Select all


Number of lines  24974
number of characters  2513411

C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\inc\win\mshtmlc.bi:
File last modified: 16-04-2016 10:20:02

 1636          845           791
 0             0             0
 0             0             0
 0             0             0
 0             0             0
 0             0             0
 0             0             0
 0             0             0
 0             0             0
 0             0             0
timer
 0.07213404547712798


Please excuse the zeros, I searched for extra spaces, but none, it was well typed.
Code I use:

Code: Select all

#include "file.bi"   
#include "crt.bi"
#include "string.bi"

'Run with the best - 32 bit -gen gas, the FreeBASIC as free from C as pos. (except msvcrt.dll or it's Linux counterpart)

Function loadfile(file As String) As String
   If Fileexists(file)=0 Then Print file;" not found":Sleep:End
    Dim As Long  f=Freefile
    Open file For Binary Access Read As #f
    Dim As String text
    If Lof(1) > 0 Then
        text = String(Lof(f), 0)
        Get #f, , text
    End If
    Close #f
    Return text
End Function

Function Tally(SomeString As String,Part As String) As Long
    Dim As zstring Ptr z=Strptr(Somestring)
    Dim As Long i=Len(Part),c
    While z
        z=iif(c,strstr(z[i],Part),strstr(z[0],Part))
         c+=1
    Wend
    Return c-1
End Function

sub getstamp(filename as string)
If FileExists( filename ) Then
    print filename;":"
  Print "File last modified: ";
  dim as double d = FileDateTime( filename )
  Print Format( d, "dd-mm-yyyy hh:mm:ss " )'rub out the :ss if you wish
Else
  Print filename;" not found"
End If
end sub
'==============

dim as string MyPath="C:\Users\User\Desktop\FreeBASIC-1.05.0-win32\inc\win\mshtmlc.bi"


Dim As String text=loadfile(MyPath)

text=lcase(text)
print
print "Number of lines ";tally(text,chr(10))
print "number of characters ";len(text)

print
 getstamp(MyPath)

print

dim as string sp
dim as long T1,T2

Dim As Double t=Timer
for n as long=1 to 10
    sp=string(n," ")
    T1=tally(text,"type"+sp)
    T2=tally(text,"end"+sp+"type")
    print T1,T2,T1-T2
next
print "timer"
print timer-t
sleep



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

Re: How to reduce the filesize of a compiled file?

Postby jj2007 » Aug 18, 2017 19:00

It seems that our *.bi versions are close but not identical. Anyway, here is the full picture:

Code: Select all

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz
counting took 0.024 seconds
0       1583 matches    [type sp case-sensitive]
1       2573 matches    [no sp cs]
2       2563 matches    [cs+full word]
3       1636 matches    [sp+case-insensitive]
4       5356 matches    [no sp cis]
5       2595 matches    [cis+fw]
6       5324 matches    [1st char case-insensitive]
7       2563 matches    [1st cis+fw]
8       0 matches       [end type sp case-sensitive]
9       0 matches       [end type sp cis]
10      845 matches     [end type cs]
11      845 matches     [end type cis]
for comparison: MS Word, case sensitive, full word: 2563-845=1718


Case 2 minus case 10 is the correct result. Source is here.

Return to “General”

Who is online

Users browsing this forum: marcov, srvaldez and 6 guests