tune.odin 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //+ignore
  2. package math_big
  3. /*
  4. Copyright 2021 Jeroen van Rijn <[email protected]>.
  5. Made available under Odin's BSD-3 license.
  6. A BigInt implementation in Odin.
  7. For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
  8. The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
  9. */
  10. import "core:fmt"
  11. import "core:time"
  12. Category :: enum {
  13. itoa,
  14. atoi,
  15. factorial,
  16. factorial_bin,
  17. choose,
  18. lsb,
  19. ctz,
  20. sqr,
  21. bitfield_extract,
  22. rm_trials,
  23. };
  24. Event :: struct {
  25. ticks: time.Duration,
  26. count: int,
  27. cycles: u64,
  28. }
  29. Timings := [Category]Event{};
  30. print_timings :: proc() {
  31. duration :: proc(d: time.Duration) -> (res: string) {
  32. switch {
  33. case d < time.Microsecond:
  34. return fmt.tprintf("%v ns", time.duration_nanoseconds(d));
  35. case d < time.Millisecond:
  36. return fmt.tprintf("%v µs", time.duration_microseconds(d));
  37. case:
  38. return fmt.tprintf("%v ms", time.duration_milliseconds(d));
  39. }
  40. }
  41. for v in Timings {
  42. if v.count > 0 {
  43. fmt.println("\nTimings:");
  44. break;
  45. }
  46. }
  47. for v, i in Timings {
  48. if v.count > 0 {
  49. avg_ticks := time.Duration(f64(v.ticks) / f64(v.count));
  50. avg_cycles := f64(v.cycles) / f64(v.count);
  51. fmt.printf("\t%v: %s / %v cycles (avg), %s / %v cycles (total, %v calls)\n", i, duration(avg_ticks), avg_cycles, duration(v.ticks), v.cycles, v.count);
  52. }
  53. }
  54. }
  55. @(deferred_in_out=_SCOPE_END)
  56. SCOPED_TIMING :: #force_inline proc(c: Category) -> (ticks: time.Tick, cycles: u64) {
  57. cycles = time.read_cycle_counter();
  58. ticks = time.tick_now();
  59. return;
  60. }
  61. _SCOPE_END :: #force_inline proc(c: Category, ticks: time.Tick, cycles: u64) {
  62. cycles_now := time.read_cycle_counter();
  63. ticks_now := time.tick_now();
  64. Timings[c].ticks = time.tick_diff(ticks, ticks_now);
  65. Timings[c].cycles = cycles_now - cycles;
  66. Timings[c].count += 1;
  67. }
  68. SCOPED_COUNT_ADD :: #force_inline proc(c: Category, count: int) {
  69. Timings[c].count += count;
  70. }