benchmark.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import times
  2. import gapbuffer
  3. # Global stats
  4. var
  5. totalTime = 0.0
  6. numBenches = 0
  7. template benchmark(name: string, body: untyped) =
  8. var t = 0.0
  9. for i in 0..100000:
  10. var start = epochTime()
  11. block:
  12. body
  13. t += epochTime() - start
  14. totalTime += t
  15. numBenches += 1
  16. echo "Benchmark `", name, "` took ~", int(t * 1000), "ms"
  17. benchmark "from 16 character string":
  18. discard "abcdefghijklmnop".toGapBuffer()
  19. var buf = "abcdefghijklmnop".toGapBuffer()
  20. benchmark "to 16 character string":
  21. discard $buf
  22. benchmark "get position":
  23. discard buf.pos()
  24. benchmark "get length":
  25. discard buf.len()
  26. benchmark "move to":
  27. buf.moveTo(10)
  28. buf.moveTo(0)
  29. benchmark "move by":
  30. buf.move(10)
  31. buf.moveTo(0)
  32. benchmark "insert/delete":
  33. buf.insert('a')
  34. buf.delete()
  35. benchmark "replace at cursor":
  36. buf[buf.pos()] = 'a'
  37. benchmark "replace elsewhere":
  38. buf[10] = 'a'
  39. benchmark "insert string":
  40. buf.insert("hello\n")
  41. benchmark "split lines":
  42. var i = 0
  43. for _ in buf.splitLines():
  44. break
  45. echo()
  46. echo numBenches, " benchmarks run"
  47. echo "Total time:\t~", int(totalTime * 1000), "ms"
  48. echo "Average time:\t~", int((totalTime * 1000) / float(numBenches)), "ms"