Created
July 12, 2016 13:14
-
-
Save X-Raym/a6460921bca6208867da0df0761cb5c6 to your computer and use it in GitHub Desktop.
Lua Local vs Global function simple banchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Time function from http://stackoverflow.com/questions/463101/lua-current-time-in-milliseconds | |
| ------------------------------------------------------------- | |
| -- TEST A | |
| -- Local Function | |
| local function Add( val ) | |
| return val + 1 | |
| end | |
| local x = os.clock() | |
| local s = 0 | |
| for i=1, 1000000 do | |
| s = Add( s ) | |
| end | |
| local result_a = os.clock() - x | |
| print("TEST A; Local Function") | |
| print(string.format("elapsed time: %.2f\n", result_a)) | |
| ------------------------------------------------------------- | |
| -- TEST B | |
| -- Global Function | |
| function AddB( val ) -- Function not local | |
| return val + 1 | |
| end | |
| local time_b = os.clock() | |
| local s = 0 | |
| for i=1, 1000000 do | |
| s = AddB( s ) | |
| end | |
| local result_b = os.clock() - time_b | |
| print("TEST B: Global Function") | |
| print(string.format("elapsed time: %.2f\n", result_b) ) | |
| ------------------------------------------------------------- | |
| -- TEST C | |
| -- No Function | |
| local time_c = os.clock() | |
| local s = 0 | |
| for i=1, 1000000 do | |
| s = s + 1 | |
| end | |
| local result_c = os.clock() - time_c | |
| print("TEST C: No Function") | |
| print(string.format("elapsed time: %.2f\n", result_c) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment