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.lua

which 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=$(date +%s)

count=1
upboundary=1000
while [[ $count -le upboundary ]]; do
    ./bisect.o
#    echo $count
    count=$((count + 1))
done

date_end=$(date +%s)

time=`expr $date_end - $date_start`

#time=$($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=$(date +%s)

count=1
upboundary=1000
while [[ $count -le upboundary ]]; do
    lua ./examples-lua/bisect.lua
#    echo $count
    count=$((count + 1))
done

date_end=$(date +%s)

time=`expr $date_end - $date_start`

#time=$($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之后的版本, 性能好些,但是不明显。 猜测跟脚本内容有关。

lua2c的更多相关文章

随机推荐

  1. Android Acitivity 生命周期

    Activity的生命周期: (1)启动Activity:系统会先调用onCreate方法,然后调用onStart方法,最后调用onResume,Activity进入运行状态. (2)当前Activi ...

  2. UILabel 的属性(用法)方法

    Label 中常用的方法属性 UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(90, 100, 140, 40)];//设置Label ...

  3. 【BZOJ】3456: 城市规划

    http://www.lydsy.com/JudgeOnline/problem.php?id=3456 题意:求n个点的无向连通图的方案.(n<=130000) #include <bi ...

  4. HDU 4433 locker(SPFA+DP)

    题目链接 去年区域赛的题目,早就看过题目了,又是过了好久了... 这题状态转移,一看就知道应该是 线性的那种,不过细节真的不好处理,一直没想出怎么搞,期间也看过题解,好像没太看懂... dp[i][j ...

  5. 如何更改tableView cell的accessoryView位置,如何让首尾的Separator不显示

    一,如何更改tableView cell的accessoryView位置 1.实则是更改不了的,因此右边总会有一个小边距. 2.可以向 cell 的 contentView 中添加按钮放在右边,与 c ...

  6. javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型

    每一个变量都有一个确定的类型表明它存储什么样的数据.js基本的数据类型有strings字符串.numbers数字.Booleans布尔类型.字符串是使用双引号或单引号包含的一串字符:数字包括整数或浮点 ...

  7. [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  8. [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间

    16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进 ...

  9. JDBC连接池。。。转载

    1. 引言  近年来,随着Internet/Intranet建网技术的飞速发展和在世界范围内的迅速普及,计算机  应用程序已从传统的桌面应用转到Web应用.基于B/S(Browser/Server)架 ...

  10. 练习一:SQLite基本操作

    一.基础知识: 运用场景: 1>应用运行需要保存一系列有一定关系有一定结构的数据(文本也可以但是存储效率低) 2>文件类型:.db(一个数据库就是一个.db文件) 3>路径:/dat ...