lua2c
lua2c
lua2c is a Lua module and utility to convert Lua 5.1 source code to C API code.
http://lua-users.org/wiki/LuaToCee
This utility converts a given Lua source file into an equivalent C source file written in terms of Lua C API calls. At least, this works for a large subset of the Lua language (see limitations below).
基于特定lua版本, 将lua代码转换为 LUA C api实现实现的c代码。 能够满足lua语言的大的子集。
代码:
https://github.com/davidm/lua2c
使用
Example usage:
lua lua2c.lua test/bisect.luawhich generates a C file similar to as shown here: [bisect.c].
可见 功能是使用lua脚本实现。
项目还提供了一个 shell 工具, 可以集成 翻译(lua-》c), 编译(c-》机器码), 执行(execute)于一体。
./clua test/bisect.lua
当然也可以只编译,不运行:
lua2c can even compile itself! (Note: the -c option compiles only without running.)
./clua -c lua2c.lua # compile lua2c binary ./lua2c examples-lua/bisect.lua # test
对比
lua2c的作用猜测是, 提升代码运行效率。
以此项目 bisect.lua 脚本为研究对象, 使用luac将其编译出一份C的版本,
与lua的版本实现做性能对比, 运行 1000次。
C版本运行脚本:
root@fqs:/home/sambadir/lua2c-master/lua2c-master# cat time_c.sh
#!/bin/bash
# while-count: display a series of numbers
date_start=count -le upboundary ]]; do
./bisect.o
# echo ((count + 1))
done
date_end=date_end - (date_start)
echo $time
lua版本运行脚本(仅仅运行语句与c不同):
root@fqs:/home/sambadir/lua2c-master/lua2c-master# cat time_lua.sh
#!/bin/bash
# while-count: display a series of numbers
date_start=count -le upboundary ]]; do
lua ./examples-lua/bisect.lua
# echo ((count + 1))
done
date_end=date_end - (date_start)
echo $time
虽然脚本中有计算时间的部分, 但是只能达到秒的级别。 为精确计算时间, 我们使用系统的 time工具。
time /bin/bash time_c.sh
结果:
real 0m8.313s
user 0m0.464s
sys 0m0.756s
-----------------------
time /bin/bash time_lua.sh
结果:
real 0m8.770s
user 0m0.720s
sys 0m0.752s
=== 总体上, 是翻译成c之后的版本, 性能好些,但是不明显。 猜测跟脚本内容有关。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)