NO

Author Topic: Optimizing for file size  (Read 3978 times)

Eggy

  • Guest
Optimizing for file size
« on: August 01, 2016, 12:19:06 PM »
To brush up on my C I am playing some code golf in C.
I found an interesting old one on SE about translating text to Morse code: http://stackoverflow.com/questions/1352587/convert-a-string-into-morse-code

Here is somebody's C from that link:

Code: [Select]
main(c){for(;c=c?c:(c=toupper(getchar())-32)?c<0?1:
"\x95#\x8CKa`^ZRBCEIQiw#S#nx(37+$6-2&@/4)'18=,*%.:0;?5"
[c-12]-34:-3;c/=2)putchar(c/2?46-c%2:32);}

Pelles produces a 26KB executable (64-bit) or 19KB (32-bit), but when I compile with the same standard in TCC I get s 2048 Byte EXE.

I have chosen the optimize for size pull down in Pelles already. What is it including?

- .... .- -. -.- ... / ..-. --- .-. / -.-- --- ..- .-. / .... . .-.. .--.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Optimizing for file size
« Reply #1 on: August 01, 2016, 02:12:09 PM »
Pelles produces a 26KB executable (64-bit) or 19KB (32-bit), but when I compile with the same standard in TCC I get s 2048 Byte EXE.

I have chosen the optimize for size pull down in Pelles already. What is it including?
C runtime library, CRT.

TinyC uses msvcrt.dll.

You can compile and link that example with msvcrt.dll too and then exe sizes are 2048/3072 bytes.

Look here
« Last Edit: August 02, 2016, 11:01:33 AM by TimoVJL »
May the source be with you

Scripter

  • Guest
Re: Optimizing for file size
« Reply #2 on: August 01, 2016, 02:16:28 PM »
To brush up on my C I am playing some code golf in C.
I found an interesting old one on SE about translating text to Morse code: http://stackoverflow.com/questions/1352587/convert-a-string-into-morse-code

Here is somebody's C from that link:

Code: [Select]
main(c){for(;c=c?c:(c=toupper(getchar())-32)?c<0?1:
"\x95#\x8CKa`^ZRBCEIQiw#S#nx(37+$6-2&@/4)'18=,*%.:0;?5"
[c-12]-34:-3;c/=2)putchar(c/2?46-c%2:32);}

Pelles produces a 26KB executable (64-bit) or 19KB (32-bit), but when I compile with the same standard in TCC I get s 2048 Byte EXE.

I have chosen the optimize for size pull down in Pelles already. What is it including?

- .... .- -. -.- ... / ..-. --- .-. / -.-- --- ..- .-. / .... . .-.. .--.

First of all, whoever wrote that source code should be taken out and shot... Yes it is a trivial example and it might  even work but pity the guy who comes along 5 years later and has to make a change to it.

Pelles C defaults to static linking which includes the entire stdlib.lib file into each executable. In larger projects this is of little or no consequence but in something as tiny as the example it sticks out like a sore thumb. 

In Project Settings select "Multithreaded DLL" and you should see a fair reduction in the size of the EXE file... but that's a false gain because it still has to load the DLL when it runs... Just as TCC has to load the msvcrt.dll to run.

Take a look at their memory usage in Task Manager... you'll see what I mean. A smaller EXE does not equate to less memory used.







Eggy

  • Guest
Re: Optimizing for file size
« Reply #3 on: August 02, 2016, 09:57:47 AM »
Thanks. I am not worried about the actual size of this program; I was just curious, and now I know.
Great tip about memory usage, and file size, Scripter!

Scripter: It is an example of code golf where you try and write the shortest code to accomplish the game task. I am sure this guy knows C inside/out.
I myself use J to compete. It is naturally concise. For example, here is an average function in J:

Code: [Select]
avg=: +/%#
+/ inserts a '+' between args given to function
% is the divide function in J
# is the tally or number of args

Code: [Select]
    avg 1 2 3
2

J is very good for mathematics. It is an array-based language. Arrays are its fundamental unit of computation, and it has been open source since 1990 or so. I think only Haskell comes close in how close the programming language models the math equation. It is an interpreted language with a REPL, but it is VERY fast. It uses C libs for BLAS and other numeric routines, and it is very simple to interface to and from C with it.


Scripter

  • Guest
Re: Optimizing for file size
« Reply #4 on: August 02, 2016, 03:59:59 PM »
Scripter: It is an example of code golf where you try and write the shortest code to accomplish the game task. I am sure this guy knows C inside/out.

There seems little doubt buddy was a C-mistro but outside of scenarios such as the "Obfuscated C" contest I sincerely hope he's more careful how he writes his source code.  ;D

For sure I wouldn't want to work with him!

Quote
I myself use J to compete. It is naturally concise. For example, here is an average function in J:

Code: [Select]
avg=: +/%#
+/ inserts a '+' between args given to function
% is the divide function in J
# is the tally or number of args

Code: [Select]
    avg 1 2 3
2

J is very good for mathematics. It is an array-based language. Arrays are its fundamental unit of computation, and it has been open source since 1990 or so. I think only Haskell comes close in how close the programming language models the math equation. It is an interpreted language with a REPL, but it is VERY fast. It uses C libs for BLAS and other numeric routines, and it is very simple to interface to and from C with it.

Ahhh... good to know. I may look into it a bit further.