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. ACM Longest Repeated Sequence

    Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...

  2. 洛谷 P1030 求先序排列 Label:None

    题目描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). 输入输出格式 输入格式: 2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序 ...

  3. HDU - Pseudoforest

    Description In graph theory, a pseudoforest is an undirected graph in which every connected componen ...

  4. 【bzoj2179】FFT快速傅立叶 FFT模板

    2016-06-01  09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...

  5. BZOJ4518: [Sdoi2016]征途

    Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜 ...

  6. Java_cookie 和session 的区别详解

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...

  7. VS2010 C++环境下DLL和LIB文件目录及名称修改

    VS2010 C++环境下DLL和LIB文件目录及名称修改 转自:http://blog.csdn.net/archielau/article/details/8507581 DLL工程,Debug版 ...

  8. 常见的sql语句 注意点及用法【区分mysql 和Sqlserver】

    如何判断在字符串字段中是否包含某个字符串 mysql:   url:http://www.springload.cn/springload/detail/399 mysql> SELECT * ...

  9. 【软件工程实践一】git使用心得

    第一次软工实践,我们需要做的是学习如何使用github,并将本地库的文件添加到远程库中,以下是我进行实践的工程. [一.git的安装及准备工作] 首先从http://msysgit.github.io ...

  10. hdu2612.。。。

    原题链接 水了一天bfs了 题意:2个人分别从Y,M出发,到达其中任意一个“@” (图中有多个“@”点),2人到达的必须是同一个“@”点,求最短的路程和 思路:bfs搜2次,用一个2维数组记录到达各个 ...