kahlua java上的lua脚本介绍及性能测试

kahlua是一款基于CLDC1.1且非常小巧的Lua解释器,它很容易扩展。只需要配合一个Lua编译器,就可以执行编译后的Lua源代码。

也可以用在j2se上,而且速度还非常快!!支持把lua文件的编译执行,大家可以到http://www.oschina.net/p/kahlua 下载项目和交流此项目

以下是一个测试例子

java 代码

  1. public static void main ( String [ ] args ) throws FileNotFoundException,
  2.     IOException {
  3.    long l = System. currentTimeMillis ( );
  4.   LuaState state = new LuaState ( System. out );
  5.   UserdataArray. register (state );
  6.   OsLib. register (state );
  7.   LuaCompiler. register (state );
  8.   state. getEnvironment ( ). rawset ( "setPosition", new JavaFunctionSetPosition ( ) );
  9.    // state = runLua(dir, state, new File(dir, "stdlib.lbc"));
  10.    File testhelper = new File ( "E:/work/zincscript/res/test.lua" );
  11.   LuaClosure closure = LuaCompiler. loadis ( new FileInputStream (testhelper ), testhelper. getName ( ), state. getEnvironment ( ) );
  12.    for ( int i = 0; i < 10; i++ ) {
  13.    state. call (closure, null );
  14.    }
  15.    System. out. println ( System. currentTimeMillis ( ) – l );
  16.   }
  17.  
  18. class JavaFunctionSetPosition implements JavaFunction {
  19.  
  20.  @Override
  21.   public int call (LuaCallFrame callFrame, int nArguments ) {
  22.    // System.out.println(callFrame.get(0));
  23.    return 1;
  24.   }
  25.  
  26. }
  27.  
  1. lua 代码
  1. a= 0
  2. do print (a ) end
  3. for i = 1, 1000000 do
  4.  a = a +1
  5.  setPosition (i )
  6. end
  7. do print (a ) end
  8.  

以上意思是运行1000000次setPosition方法 , setPosition是一个java的方法。
运行结果是700ms,1000000 次才700ms非常不错了
groovy运行时间也是700ms左右

你可能感兴趣的:(kahlua java上的lua脚本介绍及性能测试)