You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main_test.go 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "github.com/csmith/aoc-2019/common"
  4. "github.com/csmith/aoc-2019/intcode"
  5. "runtime/debug"
  6. "sync"
  7. "testing"
  8. )
  9. func findOutputMany(pool *sync.Pool, input []int, target int) int {
  10. res := make(chan int)
  11. for n := 0; n < 100; n++ {
  12. for v := 0; v < 100; v++ {
  13. go func(n, v int) {
  14. if run(pool, input, n, v) == target {
  15. res <- 100*n + v
  16. }
  17. }(n, v)
  18. }
  19. }
  20. return <-res
  21. }
  22. func findOutputLinear(pool *sync.Pool, input []int, target int) int {
  23. for n := 0; n < 100; n++ {
  24. for v := 0; v < 100; v++ {
  25. if run(pool, input, n, v) == target {
  26. return 100*n + v
  27. }
  28. }
  29. }
  30. panic("Should've returned")
  31. }
  32. func Benchmark_goroutine_per_pair(b *testing.B) {
  33. debug.SetGCPercent(-1)
  34. for i := 0; i < b.N; i++ {
  35. input := common.ReadCsvAsInts("input.txt")
  36. pool := &sync.Pool{
  37. New: func() interface{} {
  38. return intcode.NewVirtualMachine(make([]int, len(input)))
  39. },
  40. }
  41. _ = run(pool, input, 12, 2)
  42. _ = findOutputMany(pool, input, 19690720)
  43. }
  44. }
  45. func Benchmark_goroutine_per_noun(b *testing.B) {
  46. debug.SetGCPercent(-1)
  47. for i := 0; i < b.N; i++ {
  48. input := common.ReadCsvAsInts("input.txt")
  49. pool := &sync.Pool{
  50. New: func() interface{} {
  51. return intcode.NewVirtualMachine(make([]int, len(input)))
  52. },
  53. }
  54. _ = run(pool, input, 12, 2)
  55. _ = findOutput(pool, input, 19690720)
  56. }
  57. }
  58. func Benchmark_no_goroutines(b *testing.B) {
  59. for i := 0; i < b.N; i++ {
  60. input := common.ReadCsvAsInts("input.txt")
  61. pool := &sync.Pool{
  62. New: func() interface{} {
  63. return intcode.NewVirtualMachine(make([]int, len(input)))
  64. },
  65. }
  66. _ = run(pool, input, 12, 2)
  67. _ = findOutputLinear(pool, input, 19690720)
  68. }
  69. }