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 矩形的个数

    矩形的个数 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3 ...

  2. Codeforces Round #228 (Div. 2) B. Fox and Cross

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  3. 【BZOJ1067】【POJ2637】降雨量

    1067: [SCOI2007]降雨量 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3004  Solved: 767[Submit][Status] ...

  4. 纪念逝去的岁月——C/C++二分查找

    代码 #include <stdio.h> int binarySearch(int iList[], int iNum, int iX, int * pPos) { if(NULL == ...

  5. C#中Thread.sleep() 【转载】

    我们可能经常会用到 Thread.Sleep 函数来使线程挂起一段时间.那么你有没有正确的理解这个函数的用法呢?思考下面这两个问题: 1.假设现在是 2008-4-7 12:00:00.000,如果我 ...

  6. linux文本操作界面 vi面板如何复制一行

    linux文本操作界面 vi面板如何复制一行 1)把光标移动到要复制的行上2)按yy3)把光标移动到要复制的位置4)按p 在vi里如何复制一行中间的几个字符?如果你要从光标处开始复制 4 个字符,则先 ...

  7. checkbox全选与反选

    用原生js跟jquery实现checkbox全选反选的一个例子 原生js: <!DOCTYPE html> <html lang="en"> <hea ...

  8. js写当鼠标悬浮及移开出现背景变化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. php继承后构造函数的特性

    在5.x版本的php中: 如果父类有构造函数,它的子类也有构造函数,那么在运行子类时就“不会执行父类的构造函数”. 要想执行父类的构造函数,需要在子类的构造函数中加上: parent::__const ...

  10. spring security使用数据库资源

    国内对权限系统的基本要求是将用户权限和被保护资源都放在数据库里进行管理,在这点上Spring Security并没有给出官方的解决方案,为此我们需要对Spring Security进行扩展.. 数据库 ...