Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://stackoverflow.com/questions/37637935/how-can-i-execute-lua-functions-from-c-without-having-to-find-them-each-time
- #include "lua.hpp"
- #include <iostream>
- #include <chrono>
- int main(int argc, char **argv) {
- lua_State* L = luaL_newstate();
- luaL_openlibs(L);
- {
- auto start = std::chrono::steady_clock::now();
- for (int i = 0; i < 1000000000; ++i)
- {
- lua_getglobal(L, "tonumber");
- lua_pop(L, 1);
- }
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double> diff = end - start;
- std::cout << (end - start).count() << " " << diff.count() << std::endl;
- }
- {
- lua_getglobal(L, "tonumber");
- int tonumber = luaL_ref(L, LUA_REGISTRYINDEX);
- auto start = std::chrono::steady_clock::now();
- for (int i = 0; i < 1000000000; ++i)
- {
- lua_rawgeti(L, LUA_REGISTRYINDEX, tonumber);
- lua_pop(L, 1);
- }
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double> diff = end - start;
- std::cout << (end - start).count() << " " << diff.count() << std::endl;
- }
- {
- lua_State* L2 = lua_newthread(L);
- lua_getglobal(L2, "tonumber");
- int tonumber = lua_gettop(L2);
- auto start = std::chrono::steady_clock::now();
- for (int i = 0; i < 1000000000; ++i)
- {
- lua_pushvalue(L2, tonumber);
- lua_xmove(L2, L, 1);
- lua_pop(L, 1);
- }
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double> diff = end - start;
- std::cout << (end - start).count() << " " << diff.count() << std::endl;
- }
- {
- lua_getglobal(L, "tonumber");
- auto start = std::chrono::steady_clock::now();
- for (int i = 0; i < 1000000000; ++i)
- {
- lua_pushvalue(L, -1);
- lua_pop(L, 1);
- }
- auto end = std::chrono::steady_clock::now();
- std::chrono::duration<double> diff = end - start;
- std::cout << (end - start).count() << " " << diff.count() << std::endl;
- }
- lua_close(L);
- return 1;
- }
- Results (time passed in seconds on right side):
- 25093427537 25.0934
- 12223462792 12.2235
- 11023562360 11.0236
- 5823560132 5.82356
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement