跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 凹语言执行速度比 Go 快一倍,体积十分之一

发表于

凹语言还在开发状态的 v0.17.0 版本针对后端输出的 wat 文件做了体积优化。以递归版本的 Fib 为例,输出的体积是 Go 的十分之一,执行速度快一倍。详细请参考凹语言主库下的 tests/bench/fib 目录文档。

Fib 测试代码

凹语言的优点可以通过一个简单的 Fibonacci 示例来说明。下面是凹语言和 Go 两种语言中实现的 fib 函数。

凹语言代码:

func init {
	println(fib(46))
}

func fib(n: int) => int {
	aux: func(n, acc1, acc2: int) => int
	aux = func(n, acc1, acc2: int) => int {
		switch n {
		case 0:
			return acc1
		case 1:
			return acc2
		default:
			return aux(n-1, acc2, acc1+acc2)
		}
	}
	return aux(n, 0, 1)
}

Go 代码:

package main

func main() {
	println(fib(46))
}
func fib(n int) int {
	var aux func(n, acc1, acc2 int) int
	aux = func(n, acc1, acc2 int) int {
		switch n {
		case 0:
			return acc1
		case 1:
			return acc2
		default:
			return aux(n-1, acc2, acc1+acc2)
		}
	}
	return aux(n, 0, 1)
}

测试结果

执行 make 输出结果如下:

$ make
wa -v
Wa version v0.17.0

go version
go version go1.21.0 darwin/amd64

wasmer -V
wasmer 4.3.7

wa build -optimize -target=wasi -output=fib_wa.wasm fib_wa.wa
GOOS=wasip1 GOARCH=wasm go build -o fib_go.wasm

du -sh fib_*.wasm
1.3M    fib_go.wasm
 12K    fib_wa.wasm

time wasmer fib_wa.wasm
1836311903
    0.12 real         0.02 user         0.03 sys
time wasmer fib_go.wasm
1836311903
    0.26 real         0.04 user         0.05 sys

总结

  • 凹语言是 v0.17.0 版本, Go 是 1.21.0 版本
  • 凹语言输出的 wasm 体积为 12KB, Go 语言输出 1.3MB 大小的 wasm, 凹语言是 Go 的 1/10 大小
  • 凹语言执行时间 0.12, Go 的执行时间是 0.26, 凹语言是 Go 的 1/2 执行时间

原文: https://wa-lang.org/smalltalk/st0051.html

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.