What's wrong with my code? Why in output memoized version are slowly? (def h 0.01) (defn…
- User
What's wrong with my code? Why in output memoized version are slowly? (def h 0.01)
(defn trapezoid [f h x1 x2] (* h (/ (+ (f x1) (f x2)) 2)))
;; Мемоизированная версия интеграции (defn integrate-memoized [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (if (zero? n) summ (recur f h (+ summ (trapezoid f h x1 x2)) (dec n)))))
(defn integral-memoized [f h] (let [memo-f (memoize f)] (fn [x] (integrate-memoized memo-f h 0 (int (/ x h))))))
;; Обычная версия интеграции без мемоизации (defn integrate-plain [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (if (zero? n) summ (recur f h (+ summ (trapezoid f h x1 x2)) (dec n)))))
(defn integral-plain [f h] (fn [x] (integrate-plain f h 0 (int (/ x h)))))
;; Версия с ленивыми последовательностями (defn integrate-lazy [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (cons summ (lazy-seq (integrate-lazy f h (+ summ (trapezoid f h x1 x2)) (inc n))))))
(defn integral-lazy [f h] (let [sequence (integrate-lazy f h 0 0)] (fn [x] (nth sequence (dec (int (/ x h)))))))
;; Интегралы для cos и sin (def integral-cosx-memoized (integral-memoized (fn [x] (Math/cos x)) h)) (def integral-sinx-memoized (integral-memoized (fn [x] (Math/sin x)) h))
(def integral-cosx-plain (integral-plain (fn [x] (Math/cos x)) h)) (def integral-sinx-plain (integral-plain (fn [x] (Math/sin x)) h))
(def integral-cosx-lazy (integral-lazy (fn [x] (Math/cos x)) h)) (def integral-sinx-lazy (integral-lazy (fn [x] (Math/sin x)) h))
(println "Сравнение производительности интегрирования:")
;; Сравнение для интеграла cosx (println "\nMem cos(x):") (time (integral-cosx-memoized 0.5)) (time (integral-cosx-memoized 1)) (time (integral-cosx-memoized 2)) (time (integral-cosx-memoized 4)) (time (integral-cosx-memoized 5))
(println "\nRegular cos(x):") (time (integral-cosx-plain 0.5)) (time (integral-cosx-plain 1)) (time (integral-cosx-plain 2)) (time (integral-cosx-plain 4)) (time (integral-cosx-plain 5))
(println "\nLazy cos(x):") (time (integral-cosx-lazy 0.5)) (time (integral-cosx-lazy 1)) (time (integral-cosx-lazy 2)) (time (integral-cosx-lazy 4)) (time (integral-cosx-lazy 5))
;; Сравнение для интеграла sinx (println "\nMem sin(x):") (time (integral-sinx-memoized 0.5)) (time (integral-sinx-memoized 1)) (time (integral-sinx-memoized 2)) (time (integral-sinx-memoized 4)) (time (integral-sinx-memoized 5))
(println "\nRegular sin(x):") (time (integral-sinx-plain 0.5)) (time (integral-sinx-plain 1)) (time (integral-sinx-plain 2)) (time (integral-sinx-plain 4)) (time (integral-sinx-plain 5))
(println "\nLazy sin(x):") (time (integral-sinx-lazy 0.5)) (time (integral-sinx-lazy 1)) (time (integral-sinx-lazy 2)) (time (integral-sinx-lazy 4)) (time (integral-sinx-lazy 5))
Output Mem cos(x): "Elapsed time: 0.93608 msecs" "Elapsed time: 14.303913 msecs" "Elapsed time: 17.430921 msecs" "Elapsed time: 1.20013 msecs" "Elapsed time: 0.864107 msecs"
- Assistant
Your code is observing slower performance for the memoized version because
memoizein Clojure caches the results of function calls based on the arguments. In your integration function, you have memoized the functionf, which isMath/cosorMath/sinin your examples. However, these functions are fast and the inputs tofare unlikely to repeat much during integration due to the continuous range of input values. - User
How can I change my code?
- Assistant
- User
No, no. I have to use memoization in my code. It;s important for me because my task is to compare memoized version and regular version
- Assistant
- User
Still wrong. Maybe we can change functions that we integrate? I mean don't using sin and cos. Maybe it will help? Output Performance comparison of integration:
Memoized cos(x): "Elapsed time: 0.571534 msecs" "Elapsed time: 0.504835 msecs" "Elapsed time: 12.870375 msecs" "Elapsed time: 0.694278 msecs" "Elapsed time: 0.506806 msecs"
Regular cos(x): "Elapsed time: 0.198293 msecs" "Elapsed time: 0.153558 msecs" "Elapsed time: 0.15773 msecs" "Elapsed time: 0.246293 msecs" "Elapsed time: 0.338312 msecs"
Memoized sin(x): "Elapsed time: 0.125883 msecs" "Elapsed time: 0.14012 msecs" "Elapsed time: 0.253393 msecs" "Elapsed time: 0.306712 msecs" "Elapsed time: 0.338699 msecs"
Regular sin(x): "Elapsed time: 0.105249 msecs" "Elapsed time: 0.11187 msecs" "Elapsed time: 0.21072 msecs" "Elapsed time: 12.52192 msecs" "Elapsed time: 0.50625 msecs"
- Assistant
- User
Okay, maybe it will work. Please, make a version of my code, that will compare regular calcunation with lazy calculations (that i implemented earlier)
- Assistant