I need to optimaze my function (create a new one) with memoize. How should I do it in a…
- User
I need to optimaze my function (create a new one) with memoize. How should I do it in a rigth way? And I want to compare speed of calculations of my functions with memoization and without 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) = x2 (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] ; intergrala,b = (f(a) + f(b)) * h / 2 ; h = b - a
(* (* (+ (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))
; Use the name integral-mem
(defn -main [& args])
- Assistant
- User
Make it simple. I want to check just with using 'time' and make it in main()
- Assistant
Certainly! If you want to simplify the comparison and make it work directly within the
-mainfunction using thetimemacro, you can do so with the following code. Thetimemacro will measure how long a block of code takes to execute.Here's a simplified version of your code:
clojure - User
What if I want to compare times of all my functions? Please, write down only main()
- Assistant
Sure! To compare the execution times of all your functions (
f-linear,f-square,f-polinom,f-sin,f-exponential) and their memoized versions, you can structure the-mainfunction as follows:Here's how your
-mainfunction would look:clojure - User
I have a problem: Execution error (ClassCastException) at test0.core/-main (core.clj:36). class clojure.lang.PersistentList cannot be cast to class clojure.lang.Named (clojure.lang.PersistentList and clojure.lang.Named are in unnamed module of loader 'app')
- Assistant
The error you're encountering is due to how I constructed the
functionslist. In the previous example, I mistakenly wrapped the function names in an extra list, which caused theClassCastException.You should use a vector of keywords for easier iteration without the extra list. Below is the corrected
-mainfunction that properly defines the functions and compares their execution times. The functions will still be looked up usingresolveto call them dynamically:clojure - User
So, what's wrong? I got this output, and it shows that our way to use memoize is not correct, i guess. Sometime it works, sometime it doesn't: 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"
- Assistant