mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 04:37:47 +08:00
chore(bench): add lua benchmarks (#135)
This commit is contained in:
parent
beaff939bb
commit
aba0546c2a
@ -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,
|
||||
}
|
||||
|
||||
@ -82,6 +83,7 @@ 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')
|
||||
|
13
tests/benchmarks/factors/factors.lua
Normal file
13
tests/benchmarks/factors/factors.lua
Normal file
@ -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')
|
16
tests/benchmarks/fib/fib.lua
Normal file
16
tests/benchmarks/fib/fib.lua
Normal file
@ -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')
|
22
tests/benchmarks/list/list.lua
Normal file
22
tests/benchmarks/list/list.lua
Normal file
@ -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')
|
17
tests/benchmarks/loop/loop.lua
Normal file
17
tests/benchmarks/loop/loop.lua
Normal file
@ -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')
|
25
tests/benchmarks/primes/primes.lua
Normal file
25
tests/benchmarks/primes/primes.lua
Normal file
@ -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')
|
Loading…
Reference in New Issue
Block a user