【发布时间】:2015-12-08 10:21:33
【问题描述】:
剧透警告,这是代码出现第 6 天的第一部分。
我尝试在 Clojure 和 Scala 中解决 this 问题。 Scala 程序在我的 Macbook Air 上运行良好并在几秒钟内完成。但是 Clojure 版本需要更长的时间。这里有什么问题?
(ns adventofcode.day6
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(set! *warn-on-reflection* true)
(def ^:const grid-size 1000)
(defn make-grid []
(let [grid (make-array Long/TYPE grid-size grid-size)]
(doseq [i (range grid-size)
j (range grid-size)]
(aset grid i j -1))
grid))
(defn parse-line [l]
(let [[command left-top right-bottom]
(-> l
(string/replace "turn off" "turn-off")
(string/replace "turn on" "turn-on")
(string/replace "through " "")
(string/split #" "))
parse-coordinates (fn [s]
(let [parts (string/split s #",")]
(map #(Integer/parseInt %) parts)))
[left top] (parse-coordinates left-top)
[right bottom] (parse-coordinates right-bottom)]
[command left top right bottom]))
(defn process-line [grid command left top right bottom]
(doseq [i (range left (inc right))
j (range top (inc bottom))]
(case command
"turn-off" (aset grid i j -1)
"turn-on" (aset grid i j 1)
"toggle" (aset grid i j (* -1 (aget grid i j)))
(throw (Exception. "unrecognized command")))))
(defn count-lights [grid]
(reduce + (for [i (range grid-size)
j (range grid-size)
:let [value (aget grid ^Long i ^Long j)]
:when (pos? value)]
value)))
(defn the-answer []
(with-open [rdr (-> "input-day6.txt"
io/resource
io/reader)]
(let [grid (make-grid)]
(doseq [l (line-seq rdr)]
(apply process-line grid
(parse-line l)))
(count-lights grid))))
编辑:我已经重写了瞬态向量的解决方案,它具有相当不错的性能(在我的 Macbook Air 上为 11 秒,在我的 Macbook Pro 上为 7 秒)
(ns adventofcode.day6faster
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(set! *warn-on-reflection* true)
(def ^:const grid-size 1000)
(defn make-grid []
(vec (repeat (* grid-size grid-size) -1)))
(defn parse-line [l]
(let [[command left-top right-bottom]
(-> l
(string/replace "turn off" "turn-off")
(string/replace "turn on" "turn-on")
(string/replace "through " "")
(string/split #" "))
parse-coordinates (fn [s]
(let [parts (string/split s #",")]
(map #(Integer/parseInt %) parts)))
[left top] (parse-coordinates left-top)
[right bottom] (parse-coordinates right-bottom)]
[command left top right bottom]))
(defn set-grid! [t-grid grid-size i j v]
(let [pos (+ i (* j (dec grid-size)))]
(assoc! t-grid pos v)))
(defn update-grid! [t-grid grid-size i j f]
(let [pos (+ i (* j (dec grid-size)))]
(assoc! t-grid pos
(f (get t-grid pos)))))
(defn process-line [t-grid command left top right bottom]
(doseq [i (range left (inc right))
j (range top (inc bottom))]
(case command
"turn-off" (set-grid! t-grid grid-size i j -1)
"turn-on" (set-grid! t-grid grid-size i j 1)
"toggle" (update-grid! t-grid grid-size i j (fn [i] (* -1 i)))
(throw (Exception. "unrecognized command")))))
(defn count-lights [grid]
(count (filter pos? grid)))
(defn the-answer []
(with-open [rdr (-> "input-day6.txt"
io/resource
io/reader)]
(let [t-grid (transient (make-grid))]
(doseq [l (line-seq rdr)]
(apply process-line t-grid
(parse-line l)))
(count-lights (persistent! t-grid)))))
编辑 2:现在有循环、类型提示和单个可变数组。在我的 Macbook Air 上运行 1.5 秒!
(ns adventofcode.day6singlearray
(:require [clojure.java.io :as io]
[clojure.string :as string]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(def ^:const grid-size 1000)
(defn make-grid []
(let [l (* grid-size grid-size)
a (make-array Long/TYPE l)]
(loop [i 0]
(if (< i l)
(do (aset ^longs a i -1)
(recur (inc i)))
a))))
(defn parse-line [l]
(let [[command left-top right-bottom]
(-> l
(string/replace "turn off" "turn-off")
(string/replace "turn on" "turn-on")
(string/replace "through " "")
(string/split #" "))
parse-coordinates (fn [s]
(let [parts (string/split s #",")]
(map #(Integer/parseInt %) parts)))
[left top] (parse-coordinates left-top)
[right bottom] (parse-coordinates right-bottom)]
[command left top right bottom]))
(defn set-grid! [grid ^long i ^long j v]
(let [pos (+ i (* j (dec grid-size)))]
(aset ^longs grid pos ^long v)))
(defn toggle-grid! [grid ^long i ^long j]
(let [pos (+ i (* j (dec grid-size)))]
(aset ^longs grid pos
^long (* -1 (aget ^longs grid pos)))))
(defn process-line [grid command left top right bottom]
(let [operation (case command
"turn-off" #(set-grid! grid %1 %2 -1)
"turn-on" #(set-grid! grid %1 %2 1)
"toggle" #(toggle-grid! grid %1 %2)
(throw (Exception. "unrecognized command")))]
(loop [^long i left
^long j top]
(if (<= i ^long right)
(if (<= j ^long bottom)
(do (operation i j)
(recur i (inc j)))
(recur (inc i) top))))))
(defn count-lights [grid]
(count (filter pos? grid)))
(defn the-answer []
(with-open [rdr (-> "input-day6.txt"
io/resource
io/reader)]
(let [grid (make-grid)]
(doseq [l (line-seq rdr)]
(apply process-line grid
(parse-line l)))
(count-lights grid))))
【问题讨论】:
-
您确定是阵列访问减慢了速度吗?您是否尝试过使用
aset-long代替 aset? -
我没有你的意见来测试这个,但我怀疑
count-lights中的理解力。此外,java 2d 数组不是连续的未装箱原语,如果将 1d 数组用作展开的 2d 数组,您应该会看到性能提升(这也简化了 count-lights 的减少 - 当输入为一个简单的数组)。 -
@noisesmith 使用瞬态而不是可变数组进行了更新。现在性能还可以。
-
f 在你的循环中间引入了拳击——你可以尝试使用它来获得关于拳击的警告:
(set! *unchecked-math* :warn-on-boxed) -
@noisesmith Case 现在只检查一次。这又节省了 1-2 秒。 :)