Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env lua
- # Lua speed comparison
- local function measure (fn)
- local t0 = os.clock()
- for i = 1,10000 do
- fn()
- end
- print (os.clock() - t0 .. " seconds")
- end
- local function v1()
- local function addv1(n)
- local sum = 0
- for i=1,n do
- if i % 2 == 0 then
- sum = sum + i
- end
- end
- return sum
- end
- addv1(10000)
- end
- local function addv2(n)
- local sum = 0
- for i=1,n do
- if i % 2 == 0 then
- sum = sum + i
- end
- end
- return sum
- end
- local function v2()
- addv2(10000)
- end
- measure(v1)
- measure(v2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement