bench.lua 840 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. local Delaunay = require ('Delaunay')
  2. local Point = Delaunay.Point
  3. math.randomseed(os.time())
  4. local function newPoint()
  5. local x, y = math.random(), math.random()
  6. return Point(x * 1000, y * 1000)
  7. end
  8. local MAX_POINTS = 1e3
  9. local N_TESTS = 10
  10. local function genPoints(n)
  11. local points = {}
  12. for i = 1, n do
  13. points[i] = newPoint()
  14. end
  15. return points
  16. end
  17. local function time(f, p)
  18. local start_time = os.clock()
  19. local result = f(unpack(p))
  20. local duration = (os.clock() - start_time) * 1000
  21. assert(result~=nil, 'Unexpected output, returned nil')
  22. return duration
  23. end
  24. local function main()
  25. for i = 1, N_TESTS do
  26. local p = genPoints(MAX_POINTS)
  27. local duration = time(Delaunay.triangulate, p)
  28. print(('Test %02d: triangulating %04d points in %.2f ms'):format(i, MAX_POINTS, duration))
  29. end
  30. end
  31. main()