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.
A lot of data is debug information.