adventofcode

Happy Advent 2020! Please put answers in the pinned threads or create one if it does not exist yet. | https://github.com/adventofcode-clojurians/adventofcode-clojurians | Join the private leaderboard with code 217019-4a55b8eb
misha 2018-12-26T15:45:13.106100Z

(defn distance [[x0 y0 z0] [x1 y1 z1]]
  (+ (Math/abs (- x0 x1))
     (Math/abs (- y0 y1))
     (Math/abs (- z0 z1))))

"Elapsed time: 9046.938164 msecs"
(defn distance [[x0 y0 z0] [x1 y1 z1]]
  (+ (Math/abs ^long (- x0 x1))
     (Math/abs ^long (- y0 y1))
     (Math/abs ^long (- z0 z1))))

"Elapsed time: 291.773856 msecs"
:opieop:

fellshard 2018-12-26T16:36:22.106400Z

Yep, I've learned to turn on reflection warnings so I can fix ones in the critical path. I know CIDER will point them out when it's enabled, other tools might as well

2018-12-26T16:44:52.106900Z

I made the same discovery on the same function…

2018-12-26T16:46:16.107100Z

(defn distance [p1 p2] (reduce + (map #(Math/abs ^int (- %1 %2)) p1 p2)))

2018-12-26T16:48:04.108700Z

This was day25 where there’s a 4th dimensions and the numbers were low. I guess long is probably better in general, and spliting out the operations would also give a speed up

2018-12-26T16:48:23.108800Z

I learned this lesson with ^Character on day 5: https://github.com/thegeez/clj-advent-of-code-2018/blob/master/src/advent2018/core.clj#L389

☝️ 1
misha 2018-12-26T16:55:01.111700Z

@norman you can (defn distance [^ints p1 ^ints p2] there

misha 2018-12-26T17:02:15.111900Z

@thegeez woah, you don't mess around with namespaces :kappa:

misha 2018-12-26T17:13:52.112400Z

c++ guys are laughing at us :opieop:

2018-12-26T17:29:52.112500Z

Really?

2018-12-26T17:34:10.112700Z

I didn’t know ^ints was a thing, but it doesn’t help anything here

misha 2018-12-26T17:35:55.112900Z

actually it is way slower, but primitive hint ^ints is for arrays of primitives

misha 2018-12-26T17:40:00.113100Z

actually, ^ints even throws execution a bit off, as it expects it to be arrays, so having ^int and ^ints at the same time ends up being slower than just ^ints

gklijs 2018-12-26T18:53:58.114500Z

Performance wise? On peak performance on some of the exercises the JVM might be faster.