我创建了2个简单的“ Hello World!”程序,其中一个使用Kotlin,另一个使用Rust:
科特琳:
fun main() {
println("Hello, world!")
}
铁锈:
fn main() {
println!("Hello, world!");
}
我使用以下两种方法生成了可执行文件:
kotlinc-native main.kt
用于Kotlin,cargo build --release
用于Rust,然后使用ls -S -lh | awk '{print $5, $9}'
检查二进制大小。
我发现Kotlin本机生成的文件大小是Rust生成的文件大小的1.48倍。
为什么存在这种差异?
$ ./program.kexe
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'
835K program.kexe
43B main.kt
$ ./rust
Hello, world!
$ ls -S -lh | awk '{print $5, $9}'
565K rust
128B deps
104B rust.d
64B build
64B examples
64B incremental
64B native
此外,Rust可以优化为更小,Kotlin原生语言中是否有类似的东西?
初始设置:
$ cargo new hello_world
构建方式:
$ cargo build
=> 589,004 bytes
优化步骤1:
构建方式:
$ cargo build --release
=> 586,028 bytes
优化步骤2:
将
main.rs
的内容更改为:
use std::alloc::System;
#[global_allocator]
static A: System = System;
fn main() {
println!("Hello, world!");
}
=> 335,232 bytes
优化步骤3:
在下面添加到
Cargo.toml
中。
[profile.release]
lto = true
=> 253,752 bytes
优化步骤4:
通过以下方式执行可执行文件
$ strip target/release/hello_world
=> 177,608 bytes
因此,最终我们得到的是kotlin本机生成的文件是rust生成的文件的4.87X(〜5X)
答案 0 :(得分:3)
Rust没有垃圾收集器