Skip to content

Instantly share code, notes, and snippets.

@kungtotte
Created November 14, 2018 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kungtotte/07c9d16918653d2f8b6fc6894d93e01c to your computer and use it in GitHub Desktop.
Save kungtotte/07c9d16918653d2f8b6fc6894d93e01c to your computer and use it in GitHub Desktop.
Nim versus Rust binary sizes
fib_nim.nim:
proc fib(n: int32): int32 =
if n == 0 or n == 1:
return n
else:
return fib(n - 1) + fib(n - 2)
when isMainModule:
echo "Fib 15: ", fib(15)
echo "Fib 11: ", fib(11)
echo "Fib 19: ", fib(19)
fib_rust.rs:
fn main() {
println!("Fib 15: {}", fib(15));
println!("Fib 11: {}", fib(11));
println!("Fib 19: {}", fib(19));
}
fn fib(n: i32) -> i32 {
if n == 0 || n == 1 {
return n
} else {
return fib(n - 1) + fib(n - 2)
}
}
% nim c fib_nim.nim
% rustc fib_rust.rs
% ls -l
.rwxr-xr-x 112k thomas 14 nov 13:41 fib_nim*
.rw-r--r-- 208 thomas 14 nov 13:36 fib_nim.nim
.rwxr-xr-x 4,0M thomas 14 nov 13:41 fib_rust*
.rw-r--r-- 253 thomas 30 okt 15:34 fib_rust.rs
% strip fib_nim
% strip fib_rust
% ls -l
.rwxr-xr-x 92k thomas 14 nov 13:42 fib_nim*
.rw-r--r-- 208 thomas 14 nov 13:36 fib_nim.nim
.rwxr-xr-x 424k thomas 14 nov 13:42 fib_rust*
.rw-r--r-- 253 thomas 30 okt 15:34 fib_rust.rs
% nim c -d:release --opt:size fib_nim.nim
% ls -l fib_nim
.rwxr-xr-x 48k thomas 14 nov 13:43 fib_nim*
% strip fib_nim
% ls l fib_nim
.rwxr-xr-x 35k thomas 14 nov 13:44 fib_nim*
@hxw
Copy link

hxw commented Aug 29, 2019

On FreeBSD 12.0 with rustc rustc 1.37.0

% rustc fib.rs 
% ls -l fib
-rwxr-xr-x  1 hsw  hsw  303520 2019-08-29 12:43 fib*
% strip fib
% ls -l fib
-rwxr-xr-x  1 hsw  hsw  224600 2019-08-29 12:43 fib*

% rustc -O -C lto fib.rs
% ls -l fib
-rwxr-xr-x  1 hsw  hsw  241016 2019-08-29 12:53 fib*
% strip fib             
% ls -l fib
-rwxr-xr-x  1 hsw  hsw  194088 2019-08-29 12:53 fib*

An improvement but not near nim yet

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