|
helper2416_lua5.2移植_效率测试
[复制链接]
好吧,这几天卡壳了,没什么进展,主要是ucos移植不稳定,问题多多,后面很多无法继续,lwip也不稳定,主要是ucos的问题吧。后面有时间在继续吧,先发点别的。目前是在这个不完善的ucos上面跑的,后面我会搞到raw-os上面去,比较简单的,不太复杂,玩玩很不错的。
给大家一点关于lua的参考网站,不了解的可以了解下,效率比较高的虚拟机,脚本语言,虚拟机也比较有特色,寄存器虚拟机/堆栈虚拟机两个典型分支。
主站:
- http://www.lua.org/start.html
复制代码
嵌入式LUA:
- https://github.com/elua/elua
复制代码
测评:
- http://blog.csdn.net/tcpipstack/article/details/8259179
复制代码
好了,开始我的移植和测试,主要完成一个timer4用于统计执行效率,移植比较简单,下载源码添加到工程中将lua.c和luac.c屏蔽掉
在loslib.c中实现三个函数原型
- int system(const char *cmd){
- return 1;
- }
- time_t time(time_t * data)
- {
- time_t tm;
- return tm;
- }
- void exit(int s)
- {
- s++;
- while(1);
- }
复制代码大家可以自行实现,后面了解下elua在看需要完善哪些,我这里简单添加,不影响。
lua和c的循环加法效率测试
- /**
- *******************************************************************************
- * @file Lua_Efficiency.c
- * @author camel.shoko
- * @version v1.1
- * @date 2013/9/27
- * @brief Lua效率测试文件
- * @copyright camel.shoko@gmail.com
- *******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include "platform.h"
- #include "TIMER4.h"
- #include <math.h>
- #include "lua.h"
- #include "lauxlib.h"
- #include "lualib.h"
- #include "ucos_ii.h"
- #include "Lua_Efficiency.h"
- /* Local Variables -----------------------------------------------------------*/
- const char LUA_SCRIPT[] = {
- "function loop_add(a, b,t) "
- " local sum = 0 "
- " for i = 1, t do "
- " sum = sum + a + b "
- " end "
- " return sum "
- "end "
- " "
- };
- /* Global Functions ----------------------------------------------------------*/
- /**
- * @brief c_loop_add
- * @note C语言循环加
- * @param none
- * @retval none
- */
- uint32_t c_loop_add(int a,int b,int t)
- {
- uint32_t result=0,i=0;
-
- for (i=0;i<t;i++) {
- result = result + a + b;
- }
- return result;
- }
- /**
- * @brief lua_loop_add
- * @note Lua循环加
- * @param none
- * @retval none
- */
- int lua_loop_add(lua_State *L, const char *func_name, int x, int y, int t)
- {
- int sum;
-
- /*装载脚本*/
- lua_getglobal(L, func_name);
- /* 第一个参数 */
- lua_pushnumber(L, x);
- /* 第二个参数 */
- lua_pushnumber(L, y);
- /* 第三个参数 */
- lua_pushnumber(L, t);
-
- /* 调用函数,告知有三个参数,一个返回值 */
- lua_call(L, 3, 1);
- /* 得到结果 */
- sum = (int) lua_tointeger(L, -1);
-
- lua_pop(L, 1);
- return sum;
- }
- /**
- * @brief Lua_Efficiency_Test
- * @note Lua效率测试
- * @param none
- * @retval none
- */
- void Lua_Efficiency_Test(void)
- {
- #define US_PERCENT 15
-
- int sum, i;
- uint32_t duration = 0;
- lua_State *L = luaL_newstate();
-
- if (L == NULL) {
- DEBUG0("Cannot Create State: Not Enough Memory\r\n");
- }
-
- //64kb内存紧张,用luaL_openlibs加载所有模块,会自动退出
- luaopen_base(L);
-
- luaL_dostring(L, LUA_SCRIPT);
- DEBUG0("LUA Module Init Success\r\n");
- TIMER4_Init();
-
- for (i = 2000; i < 20000; i = i + 2000) {
-
- //计算lua循环加运算的耗时
- TIMER4_Start();
- sum = lua_loop_add(L, "loop_add", 1, 1, i);
- duration = TCNTB4_CFG - TIMER4_Stop();
- DEBUG2("--Lua_Cnt %06d Lua_Sum %06d", i, sum);
- DEBUG1(">>Frequency Duration(us): %06d\r\n", (duration*US_PERCENT));
-
- //计算c循环加运算的耗时
- TIMER4_Start();
- sum = c_loop_add(1, 1, i);
- duration = TCNTB4_CFG - TIMER4_Stop();
- DEBUG2("--C_Cnt %06d C_Sum %06d", i, sum);
- DEBUG1(">>Frequency Duration(us): %04d\r\n", (duration*US_PERCENT));
-
- }
- //关闭虚拟机
- lua_close(L);
- }
- /****************** (C) COPYRIGHT 2013 Camle.shoko ***********END OF FILE******/
复制代码
测试结果如下
其实效率还是很不错的,虚拟机这个速度相当快了,恩我使用的luaopen_base,这个是基础库,如果想使用全部的可以使用luaL_openlibs加载所有模块。
之前在M3上面测试过,用的基础模块,RAM比较吃力,在helper2416上面当然没问题了,随便用,应用也是随便做,最好是在实现一个vi类似的交互编辑。我觉得raw-os确实缺少一个脚本语言的虚拟机,当然lua我认为是非常适合的,玩家也多。后面我会弄到raw-os中,也算是一个不错的组件吧。
|
赞赏
-
2
查看全部赞赏
-
|