Rochet2

Untitled

Jun 5th, 2016
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. // http://stackoverflow.com/questions/37637935/how-can-i-execute-lua-functions-from-c-without-having-to-find-them-each-time
  2.  
  3.  
  4. #include "lua.hpp"
  5. #include <iostream>
  6. #include <chrono>
  7.  
  8. int main(int argc, char **argv) {
  9.     lua_State* L = luaL_newstate();
  10.     luaL_openlibs(L);
  11.  
  12.     {
  13.         auto start = std::chrono::steady_clock::now();
  14.  
  15.         for (int i = 0; i < 1000000000; ++i)
  16.         {
  17.             lua_getglobal(L, "tonumber");
  18.             lua_pop(L, 1);
  19.         }
  20.  
  21.         auto end = std::chrono::steady_clock::now();
  22.         std::chrono::duration<double> diff = end - start;
  23.         std::cout << (end - start).count() << " " << diff.count() << std::endl;
  24.     }
  25.  
  26.     {
  27.         lua_getglobal(L, "tonumber");
  28.         int tonumber = luaL_ref(L, LUA_REGISTRYINDEX);
  29.         auto start = std::chrono::steady_clock::now();
  30.  
  31.         for (int i = 0; i < 1000000000; ++i)
  32.         {
  33.             lua_rawgeti(L, LUA_REGISTRYINDEX, tonumber);
  34.             lua_pop(L, 1);
  35.         }
  36.  
  37.         auto end = std::chrono::steady_clock::now();
  38.         std::chrono::duration<double> diff = end - start;
  39.         std::cout << (end - start).count() << " " << diff.count() << std::endl;
  40.     }
  41.  
  42.     {
  43.         lua_State* L2 = lua_newthread(L);
  44.         lua_getglobal(L2, "tonumber");
  45.         int tonumber = lua_gettop(L2);
  46.         auto start = std::chrono::steady_clock::now();
  47.  
  48.         for (int i = 0; i < 1000000000; ++i)
  49.         {
  50.             lua_pushvalue(L2, tonumber);
  51.             lua_xmove(L2, L, 1);
  52.             lua_pop(L, 1);
  53.         }
  54.  
  55.         auto end = std::chrono::steady_clock::now();
  56.         std::chrono::duration<double> diff = end - start;
  57.         std::cout << (end - start).count() << " " << diff.count() << std::endl;
  58.     }
  59.  
  60.     {
  61.         lua_getglobal(L, "tonumber");
  62.         auto start = std::chrono::steady_clock::now();
  63.  
  64.         for (int i = 0; i < 1000000000; ++i)
  65.         {
  66.             lua_pushvalue(L, -1);
  67.             lua_pop(L, 1);
  68.         }
  69.  
  70.         auto end = std::chrono::steady_clock::now();
  71.         std::chrono::duration<double> diff = end - start;
  72.         std::cout << (end - start).count() << " " << diff.count() << std::endl;
  73.     }
  74.  
  75.     lua_close(L);
  76.     return 1;
  77. }
  78.  
  79. Results (time passed in seconds on right side):
  80. 25093427537 25.0934
  81. 12223462792 12.2235
  82. 11023562360 11.0236
  83. 5823560132 5.82356
Advertisement
Add Comment
Please, Sign In to add comment