mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-05 20:26:53 +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,
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
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