Saturday, August 22, 2009

Shrinking files

What can be done to make the executable files smaller? When we get the normal ELF file we can first strip it and then compress it in a way it will decompress during each run. We've got the binary file hello which prints hello world:

$ ls -l hello
-rwxr-xr-x 1 berdzi users 6230 2009-08-22 18:04 hello

It's 6230 bytes big. We will strip it with strip command. Strip removes all debugging symbols from the binary. They are very helpful when we want to debug the file with i.e. gdb, whereas they not necessary in normal use. Stripping them saves a lot of space:

$ strip hello
$ ls -l hello
-rwxr-xr-x 1 berdzi users 2748 2009-08-22 18:09 hello

Now the file has only 2748 bytes. To make it smaller we can use gzexe. It will compress the file in place - it will be decompressed while each execution. This saves extra bytes increasing runtime. Let's see:

$ gzexe ./hello
./hello: 53.9%
$ ls -l hello
-rwxr-xr-x 1 berdzi users 2092 2009-08-22 18:12 hello

This will create two files: hello and hello~ where the second one is the original file and if we sure the compressed version is ok, we can remove the source. We can always decompress the compressed file with -d switch.
Let's compare the execution time of compressed file and the original:

$ time ./hello~
Hello world

real 0m0.004s
user 0m0.000s
sys 0m0.004s

$ time ./hello
Hello world

real 0m0.066s
user 0m0.016s
sys 0m0.040s

This particular binary executes 16 times slower (in this case in almost unnoticable).

1 comment:

  1. Silver Jewellery creations are amazing invention for world. I like you post for jewelry here. If you have more information about topic please email me.