From aba0546c2af172687a04552cd8cfede3e5ee6af3 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Wed, 23 Jun 2021 23:12:32 -0700 Subject: [PATCH] chore(bench): add lua benchmarks (#135) --- tests/benchmarks/benchmarks.py | 12 +++++++----- tests/benchmarks/factors/factors.lua | 13 +++++++++++++ tests/benchmarks/fib/fib.lua | 16 ++++++++++++++++ tests/benchmarks/list/list.lua | 22 ++++++++++++++++++++++ tests/benchmarks/loop/loop.lua | 17 +++++++++++++++++ tests/benchmarks/primes/primes.lua | 25 +++++++++++++++++++++++++ 6 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 tests/benchmarks/factors/factors.lua create mode 100644 tests/benchmarks/fib/fib.lua create mode 100644 tests/benchmarks/list/list.lua create mode 100644 tests/benchmarks/loop/loop.lua create mode 100644 tests/benchmarks/primes/primes.lua diff --git a/tests/benchmarks/benchmarks.py b/tests/benchmarks/benchmarks.py index 21918e6..ccbbf0f 100644 --- a/tests/benchmarks/benchmarks.py +++ b/tests/benchmarks/benchmarks.py @@ -29,8 +29,9 @@ BENCHMARKS = ( ## Map from file extension to it's interpreter, Will be updated. INTERPRETERS = { '.pk' : None, + '.lua' : None, '.wren' : None, - '.rb' : None, + '.js' : None, '.rb' : None, } @@ -48,12 +49,12 @@ def run_all_benchmarsk(): dir = join(THIS_PATH, benchmark) for file in os.listdir(dir): file = abspath(join(dir, file)) - + ext = get_ext(file) ## File extension. if ext not in INTERPRETERS: continue lang, interp = INTERPRETERS[ext] if not interp: continue - + print("%-10s : "%lang, end=''); sys.stdout.flush() result = _run_command([interp, file]) time = re.findall(r'elapsed:\s*([0-9\.]+)\s*s', @@ -82,9 +83,10 @@ def update_interpreters(): global INTERPRETERS INTERPRETERS['.pk'] = _find_interp('pocketlang', pocket) INTERPRETERS['.wren'] = _find_interp('wren', 'wren') + INTERPRETERS['.lua'] = _find_interp('lua', 'lua') INTERPRETERS['.rb'] = _find_interp('ruby', 'ruby') INTERPRETERS['.py'] = _find_interp('python', python) - INTERPRETERS['.js'] = _find_interp('javascript', 'node') + INTERPRETERS['.js'] = _find_interp('javascript', 'node') ## This will return the path of the pocket binary (on different platforms). ## The debug version of it for enabling the assertions. @@ -151,6 +153,6 @@ def error_exit(msg): os.system('') ## This will enable ANSI codes in windows terminal. print(COLORS['RED'] + 'Error: ' + msg + COLORS['END'], end='') sys.exit(1) - + if __name__ == '__main__': main() diff --git a/tests/benchmarks/factors/factors.lua b/tests/benchmarks/factors/factors.lua new file mode 100644 index 0000000..b8e8908 --- /dev/null +++ b/tests/benchmarks/factors/factors.lua @@ -0,0 +1,13 @@ +local start = os.clock() + +local factors = {} +local N = 50000000 + +for i=1,N do + if N % i == 0 then + table.insert(factors, i) + end +end + +local seconds = os.clock() - start +print('elapsed: ' .. seconds .. 's') diff --git a/tests/benchmarks/fib/fib.lua b/tests/benchmarks/fib/fib.lua new file mode 100644 index 0000000..cde8174 --- /dev/null +++ b/tests/benchmarks/fib/fib.lua @@ -0,0 +1,16 @@ +local function fib(n) + if n < 2 then + return n + else + return fib(n - 1) + fib(n - 2) + end +end + +local start = os.clock() + +for i=1, 10 do + print(fib(30)) +end + +local seconds = os.clock() - start +print('elapsed: ' .. seconds .. 's') diff --git a/tests/benchmarks/list/list.lua b/tests/benchmarks/list/list.lua new file mode 100644 index 0000000..18689f7 --- /dev/null +++ b/tests/benchmarks/list/list.lua @@ -0,0 +1,22 @@ +local function reverse(arr) + local len = #arr + local max = math.floor(len / 2) + + for i=1, max do + local idx = len + 1 - i + arr[i], arr[idx] = arr[idx], arr[i] + end +end + +local start = os.clock() +local N = 20000000 +local list = {} + +for i=1, N do + list[i] = i +end + +reverse(list) + +local seconds = os.clock() - start +print('elapsed: ' .. seconds .. 's') diff --git a/tests/benchmarks/loop/loop.lua b/tests/benchmarks/loop/loop.lua new file mode 100644 index 0000000..ebd9485 --- /dev/null +++ b/tests/benchmarks/loop/loop.lua @@ -0,0 +1,17 @@ +local start = os.clock() + +local list = {} +for i = 1, 10000000 do + list[i] = i - 1 +end + +local sum = 0 + +for i = 1, #list do + sum = sum + list[i] +end + +print(sum) + +local seconds = os.clock() - start +print('elapsed: ' .. seconds .. 's') diff --git a/tests/benchmarks/primes/primes.lua b/tests/benchmarks/primes/primes.lua new file mode 100644 index 0000000..14e44b9 --- /dev/null +++ b/tests/benchmarks/primes/primes.lua @@ -0,0 +1,25 @@ +local function isPrime(n) + if n < 2 then + return false + end + for i=2, n-1 do + if n % i == 0 then + return false + end + end + return true +end + +local start = os.clock() + +local N = 30000 +local primes = {} + +for i = 0, N - 1 do + if isPrime(i) then + table.insert(primes, i) + end +end + +local seconds = os.clock() - start +print('elapsed: ' .. seconds .. 's')