Skip to content

Instantly share code, notes, and snippets.

@danthedaniel
Last active August 25, 2018 23:46
Show Gist options
  • Save danthedaniel/c7b181f7bc543ee9c37cfd45df5f8856 to your computer and use it in GitHub Desktop.
Save danthedaniel/c7b181f7bc543ee9c37cfd45df5f8856 to your computer and use it in GitHub Desktop.
Comparison of Crystal and C binary sizes

Crystal Source Code:

def factorial(n)
    n < 2 ? 1 : n * factorial(n - 1)
end

if ARGV.size > 0
    n = ARGV.first.to_u64
    puts factorial(n)
else
    puts "No arguments provided"
    exit 1
end

C Source Code:

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> 

uint64_t factorial(uint64_t n) {
    return (n < 2) ? 1 : n * factorial(n - 1);
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("No arguments provided\n");
        return 1;
    } else {
        uint64_t n = strtol(argv[1], NULL, 10);
        printf("%lu\n", factorial(n));
        return 0;
    }
}

It can be seen that the binary produced by Crystal is significantly larger than the C binary:

-rwxr-xr-x 1 daniel daniel    7168 Jun 24 19:46 hello_c
-rw-r--r-- 1 daniel daniel     385 Jun 24 19:49 hello.c
-rwxr-xr-x 1 daniel daniel 1270216 Jun 24 20:02 hello_cr
-rw-r--r-- 1 daniel daniel     177 Jun 24 20:01 hello.cr

177x larger in fact.

@danthedaniel
Copy link
Author

Thanks for the info, @faustinoaq

Copy link

ghost commented Aug 25, 2018

@faustinoaq doing god's work 🗡️ ty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment