Executable size 11x larger than C's?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Bluepixel
Posts: 1
Joined: Sep 01, 2020 12:03

Executable size 11x larger than C's?

Post by Bluepixel »

Hi there, I'm a big retro enthusiast, especially when it comes to old BASIC computers and Assembly.

Now I have been enjoying myself with various retro computers, writing both Assembly and a bit of BASIC.
But when it comes to writing serious projects I still use, and enjoy C. A while ago I came across this
language, but never bothered trying any 'modern' BASIC. However out of curiosity I installed the compiler
with my trusty package manager, and tried compiling a simple Hello World program to see if the claims-
(rated close in speed with GCC), were actually true. And the result was quite shocking. My executable
was over 11x the size of my C Compiled executable! My C executable was 4 blocks (1 block=512b),
however, Freebasic's executable was over 44 blocks! I suspected this might be because of some sort of
internal initializations, however I tried various optimization methods, but nothing seemed to do anything
significant. I tried disassembling the code and comparing it to C's assembly, and it was just a mess..

Now don't get me wrong, i'm not here to 'shame' the language, instead I would like to see if there are
any ways to reduce the executable size to something more reasonable, since I was quite excited to see
a tool that has my 3 favorite languages, all in one. BASIC of course, C for external libraries, and then
Assembly for inline support,. And besides being a tool for hobbyists, what are your thoughts of 'modern'
BASIC languages and it's capabilities compared to lower level languages such as C?

Thanks for the help,
Cheers! :]
angros47
Posts: 2329
Joined: Jun 21, 2005 19:04

Re: Executable size 11x larger than C's?

Post by angros47 »

The subject has already been discussed many times, here, for example: viewtopic.php?f=4&t=22664

I hope this helps.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Executable size 11x larger than C's?

Post by jj2007 »

Hi Bluepixel,

Welcome to the forum. I share your wish for small executables, and I also program mostly in Assembly and FreeBasic.

The shortest Print "Hello World" in standard Win32 takes 1024 bytes - written in Assembly.
With QT, the shortest Print "Hello World" standalone executable is about 8MB.

I wouldn't touch QT, but it's a matter of taste. Between the two extremes, many BASIC dialects require something like 20-200kBytes for a Print "hello World". For 32-bit FreeBasic, it's 24kB, for MasmBasic it's 27.5kB, for 64-bit FreeBasic it's 29kB.

Does it matter?
- loading a 1MB file from a trusty old hard disk takes less than one millisecond
- Windows takes up 20GB or so, depending on the version (Win 3.1: 6MB)

It is true that C programs can be smaller, but don't compare apples with oranges. Any BASIC dialect has a string engine, can create and work with arrays etc - there are plenty of useful functions under the hood that a C programmer must create for himself. This "infrastructure" takes up some space, typically around 30kB. Just accept it ;-)

P.S., Hello World in 8 bytes:

Code: Select all

.model tiny
.code
	org 100h
start:
	mov ah, 09h	; write string to STDOUT
	mov dx, 82h	; get command line
	int 21h		; show it... ;-)
	ret
end start
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: Executable size 11x larger than C's?

Post by caseih »

Like jj2007 says, the difference in size is the size of the FB runtime library which is statically-linked into your binary. The linker will remove uncalled routines from it, but string library takes up some space, as do the routines for setting up the FB runtime. Your C binary is only dynamically-linked to the your C standard library, which is actually quite large, or can be large. FB's binaries are also linked to this same library (glibc).

By the way, on my Linux machine a simple printf hello world is about 20k, and the FB-produced version (using printf again, not the FB runtime) is 26k.
Pim Scheffers
Posts: 54
Joined: Jun 29, 2014 17:15

Re: Executable size 11x larger than C's?

Post by Pim Scheffers »

Hello Bluepixel,

As stated above, the FB runtime library gives you access to all the 'good' stuff BASIC has to offer. The fact that you have C++ like string handling, procedural / object oriented features and a very capable graphics library, all statically linked to your Binary adds to the executable size.

An example:

FreeBasic (graphics app <libFB>, <fbgfx.bi>):
A completely stand-alone executable:
A 'Hello, World!' program on my windows (64bit) box: ~111 KB.

FreeBasic (console app <libFB>):
A completely stand-alone executable:
A 'Hello, World!' program on my windows (64bit) box: ~30 KB.

C++ (console app inc: <iostream>)
A completely stand-alone executable compiled with Code::Blocks GCC C++, statically linked, O3 optimized, release mode and symbols stripped:
A 'Hello, World!' program on my windows (64bit) box: ~819 KB.

C (console app inc: <stdlib>, <stdio>)
A completely stand-alone executable compiled with Code::Blocks GCC C, statically linked, O3 optimized, release mode and symbols stripped:
A 'Hello, World!' program on my windows (64bit) box: ~15 KB.

So FreeBasic is actually doing a great job!
UEZ
Posts: 994
Joined: May 05, 2017 19:59
Location: Germany

Re: Executable size 11x larger than C's?

Post by UEZ »

Here my try to create a compiled exe as small as possible -> viewtopic.php?f=6&t=28543 (Windows only).,

Check out 9th post where I used Crinkler to crunch it to 2kb to produce this gfx:
Image
aurelVZAB
Posts: 667
Joined: Jul 02, 2008 14:55
Contact:

Re: Executable size 11x larger than C's?

Post by aurelVZAB »

ahh look at FP exe size..
in o2 basic is just 27kB
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Executable size 11x larger than C's?

Post by Vortex »

Hi Bluepixel,

You can create small FreeBASIC executables by turning off the run-time library. Here is an example :

viewtopic.php?f=6&t=28346
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Executable size 11x larger than C's?

Post by jj2007 »

See TinyIDE for FreeBasic for a 7168 bytes FreeBasic editor.
Post Reply