I wanted to use memoization, but it doesn't work as I expected (I thought it will…
- User
I wanted to use memoization, but it doesn't work as I expected (I thought it will optimize in every case, but it optimizes not in every case). Maybe I use memeoize wrong? Output: Without memoization... "Elapsed time: 1.1064 msecs" With memoization... "Elapsed time: 2.8073 msecs"
Calculating integral for function: test0.core$f_square@3bd55d8 Without memoization... "Elapsed time: 362.0609 msecs" With memoization... "Elapsed time: 185.2293 msecs"
Calculating integral for function: test0.core$f_polinom@7e0b9178 Without memoization... "Elapsed time: 1.0207 msecs" With memoization... "Elapsed time: 2.2324 msecs"
Calculating integral for function: test0.core$f_sin@61942c1 Without memoization... "Elapsed time: 9.4644 msecs" With memoization... "Elapsed time: 1.006 msecs"
Calculating integral for function: test0.core$f_exponential@6f63c44f Without memoization... "Elapsed time: 0.4003 msecs" With memoization... "Elapsed time: 0.7844 msecs"
My code: (ns test0.core (:gen-class) (:use clojure.test))
(defn f-linear [x] x) ; f(x) = x (defn f-square [x] (Thread/sleep 10) (* x x)) ; f(x) = x^2 (defn f-polinom [x] (+ (* 2 x x) (* 3 x) 1)) ; f(x) = 2 * x^2 + 3 * x + 1 (defn f-sin [x] (Math/sin x)) ; f(x) = sin(x) (defn f-exponential [x] (Math/exp x)) ; f(x) = e^x
(defn trapezoid [f a b] (* (* (+ (f a) (f b)) (- b a)) 0.5))
(defn integral [f a b h] (if (< a b) (+ (trapezoid f a (+ a h)) (integral f (+ a h) b h)) 0))
; Memoized version of the integral function (defn integral-mem [f a b h] (if (< a b) (+ (trapezoid (memoize f) a (+ a h)) (integral (memoize f) (+ a h) b h)) 0))
(defn -main [& args] (let [a 0 b 10 h 1 functions [f-linear f-square f-polinom f-sin f-exponential]] ; List of functions]
code(doseq [f functions] (println "Calculating integral for function:" (str f)) (println "Without memoization...") (time (integral f a b h)) (println "With memoization...") (time (integral-mem f a b h)) (println)))) ; Add an empty print line for better readability - Assistant
- User
Still doesn't work in every case: Without memoization... "Elapsed time: 0.8086 msecs" With memoization... "Elapsed time: 5.6995 msecs"
Calculating integral for function: test0.core$f_square@7e0b9178 Without memoization... "Elapsed time: 345.7368 msecs" With memoization... "Elapsed time: 174.8049 msecs"
Calculating integral for function: test0.core$f_polinom@6f63c44f Without memoization... "Elapsed time: 0.5704 msecs" With memoization... "Elapsed time: 4.2933 msecs"
Calculating integral for function: test0.core$f_sin@408a247c Without memoization... "Elapsed time: 0.422 msecs" With memoization... "Elapsed time: 2.3354 msecs"
Calculating integral for function: test0.core$f_exponential@62a8fd44 Without memoization... "Elapsed time: 1.0912 msecs" With memoization... "Elapsed time: 0.976 msecs"
- Assistant
- User
Still doen't work: Without memoization... "Elapsed time: 1.1288 msecs" With memoization... "Elapsed time: 1.9425 msecs"
Calculating integral for function: test0.core$f_square@4a60ee36 Without memoization... "Elapsed time: 320.8869 msecs" With memoization... "Elapsed time: 169.4021 msecs"
Calculating integral for function: test0.core$f_polinom@58faa93b Without memoization... "Elapsed time: 1.2571 msecs" With memoization... "Elapsed time: 2.1252 msecs"
Calculating integral for function: test0.core$f_sin@5f212d84 Without memoization... "Elapsed time: 2.9689 msecs" With memoization... "Elapsed time: 1.4572 msecs"
Calculating integral for function: test0.core$f_exponential@127d7908 Without memoization... "Elapsed time: 0.6306 msecs" With memoization... "Elapsed time: 1.1712 msecs"
- Assistant
- User
An error occurres: Syntax error (UnsupportedOperationException) compiling recur at (test0\core.clj:36:35). Can only recur from tail position
- Assistant
- User
Still doesn't work: Without memoization... "Elapsed time: 0.5011 msecs" With memoization... "Elapsed time: 2.3637 msecs"
Calculating integral for function: test0.core$f_square@4ee33af7 Without memoization... "Elapsed time: 327.3662 msecs" With memoization... "Elapsed time: 312.0653 msecs"
Calculating integral for function: test0.core$f_polinom@18388a3c Without memoization... "Elapsed time: 0.9074 msecs" With memoization... "Elapsed time: 6.7533 msecs"
Calculating integral for function: test0.core$f_sin@1d035be3 Without memoization... "Elapsed time: 2.8204 msecs" With memoization... "Elapsed time: 1.3097 msecs"
Calculating integral for function: test0.core$f_exponential@4a60ee36 Without memoization... "Elapsed time: 1.1611 msecs" With memoization... "Elapsed time: 0.3768 msecs"
- Assistant