Detailed description C calls lua script Efficiency Test

Source: Internet
Author: User

CCallLua scriptEfficiencyTestIs the content to be introduced in this article. The following code usesCLanguage as the benchmark,TestNowCCallLuaLoop and loop callLua. The conclusion is that do not traverse frequentlyC/LuaBoundary.

 
 
  1. # Include <time. h>
  2.  
  3. Extern "C"
  4. {
  5. # Include "lua. h"
  6. # Include "lualib. h"
  7. # Include "lauxlib. h"
  8. }/* Lua interpreter pointer */
  9.  
  10. Const char LUA_SCRIPT [] =
  11. "Function loop_add (a, B )"
  12. "Local sum = 0"
  13. "For I = 1, 10000000 do"
  14. "Sumsum = sum + a + B"
  15. "End"
  16. "Return sum"
  17. "End"
  18. ""
  19. "Function add (a, B )"
  20. "Return a + B"
  21. "End"
  22. ;
  23.  
  24. // The functions in the lua script are called by C.
  25. Int use_lua_add (lua_State * L, const char * func_name, int x, int y)
  26. {
  27. Int sum;/* obtain the Lua function by name */
  28. Lua_getglobal (L, func_name);/* The first parameter */
  29. Lua_pushnumber (L, x);/* second parameter */
  30. Lua_pushnumber (L, y);/* call the function to inform that there are two parameters, one returned value */
  31. Lua_call (L, 2, 1);/* get the result */
  32. Sum = (int) lua_tointeger (L,-1 );
  33. Lua_pop (L, 1 );
  34. Return sum;
  35. }
  36.  
  37. Int main ()
  38. {
  39. Int I, sum = 0;
  40. Clock_t tStart, tStop;
  41.  
  42. Lua_State * L = lua_open ();/* opens Lua */
  43. LuaL_openlibs (L );
  44. If (luaL_dostring (L, LUA_SCRIPT) // Run lua script
  45. {
  46. Printf ("run script failed/n ");
  47. Lua_close (L );
  48. Return-1;
  49. }
  50.  
  51. Sum = 0;
  52. TStart = clock ();
  53. For (I = 0; I <10000000; I ++)
  54. {
  55. Sum + = 1 + 1;
  56. }
  57. TStop = clock ();
  58. Printf ("C ++: % dms./nThe sum is % u./n ",
  59. (TStop-tStart) * 1000/CLOCKS_PER_SEC, sum );
  60.  
  61. Sum = 0;
  62. TStart = clock ();
  63. Sum = use_lua_add (L, "loop_add", 1, 1 );
  64. TStop = clock ();
  65. Printf ("Lua loop_add: % dms./nThe sum is % u./n ",
  66. (TStop-tStart) * 1000/CLOCKS_PER_SEC, sum );
  67. Sum = 0;
  68. TStart = clock ();
  69. For (I = 0; I <10000000; I ++)
  70. {
  71. Sum + = use_lua_add (L, "add", 1, 1 );
  72. }
  73. TStop = clock ();
  74. Printf ("Loop lua add: % dms./nThe sum is % u./n ",
  75. (TStop-tStart) * 1000/CLOCKS_PER_SEC, sum );
  76. Lua_close (L );
  77. Return 0;
  78. }

Running result:

 
 
  1. C++: 31ms.  
  2. The sum is 20000000.  
  3. Lua loop_add: 437ms.  
  4. The sum is 20000000.  
  5. Loop lua add: 2360ms.  
  6. The sum is 20000000. 

Summary: DetailsCCallLua scriptEfficiencyTestI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.