【发布时间】:2025-11-22 10:35:01
【问题描述】:
我创建了 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 native 生成的文件是 Rust 生成的文件大小的 1.48X。
为什么会存在这种差异?
$ ./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 native 有类似的东西吗?
初始设置:
$ cargo new hello_world
构建:
$ cargo build
=> 589,004 bytes
优化步骤一:
构建:
$ 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 native 生成的文件是 rust 生成的文件的 4.87X (~ 5X)
【问题讨论】:
-
因为这两种语言具有完全不同的架构、运行时和成熟度级别。你期待什么答案?
-
LLVM 是一种生成可执行代码的机制。它不会以任何方式影响语言运行时。
-
两者都可能基于 LLVM,但这只是 yole 提到的生成器。标准库不同,语言根本不同,结果中包含不同的东西,因此可执行文件的大小也会不同。这就像将 Kotlin Native 与 Java 的
.jar进行比较;当然,即使使用相同的程序,大小也会有所不同。 -
@HasanAYousef 查看我的编辑,以了解下次该做什么。这需要我一些时间,您可以通过简单的复制/粘贴来完成此操作
标签: kotlin kotlin-native