clay icon indicating copy to clipboard operation
clay copied to clipboard

display execution time

Open daslu opened this issue 1 year ago • 3 comments

Zulip discussion: https://clojurians.zulipchat.com/#narrow/stream/422115-clay-dev/topic/Idea.3A.20display.20execution.20time

daslu avatar Sep 13 '24 12:09 daslu

@daslu Hi, my initial thought was that we can capture the output just like with-out-str, and I tweaked the complete function to concat the output and the eval result like this:

-(defn complete [{:as note
-                 :keys [comment? code form value]}]
+(defn complete
+  "Complete `note` by evaluating its `code` or `form`."
+  [{:as note
+    :keys [comment? code form value]}]
   (-> (if (or value comment?)
         note
-        (assoc note
-          :value (cond code (-> code
-                                read-string
-                                eval
-                                deref-if-needed)
-                       form (-> form
-                                eval
-                                deref-if-needed))))
+        (let [new-out (new java.io.StringWriter)]
+          (binding [*out* new-out]
+            (assoc note
+                   :value (->> (cond code (-> code
+                                             read-string
+                                             eval
+                                             deref-if-needed)
+                                    form (-> form
+                                             eval
+                                             deref-if-needed))
+                               (str new-out "\n")
+                               )))))
       (cond-> (not comment?)

It turned out this doesn't work as the concat'ed string will be treated as the whole value, that is, "\"Elapsed time: 0.027495 msecs\"\n\n6" will be the value for (time (+ 1 2 3)).

What do you think will be a better way to achieve the goal? Thanks.

whatacold avatar Sep 22 '24 15:09 whatacold

Thanks for looking into this.

Probably we should measure times ourselves, and probably this should happen in the scicloj.clay.v2.make namespace.

Anyway, first, we should define the goal better. We should clarify what times should actually be measured (evaluation? rendering? etc.) in various workflows.

daslu avatar Sep 22 '24 22:09 daslu

Thanks for replying.

If we measure the time ourselves, isn’t it not flexible enough? For example, one may want to demonstrate println to show its output and result.

On Mon, Sep 23, 2024 at 06:03 Daniel Slutsky @.***> wrote:

Thanks for looking into this.

Probably we should measure times ourselves, and probably this should happen in the scicloj.clay.v2.make namespace.

Anyway, first, we should define the goal better. We should clarify what times should actually be measured (evaluation? rendering? etc.) in various workflows.

— Reply to this email directly, view it on GitHub https://github.com/scicloj/clay/issues/157#issuecomment-2366984180, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKVIOXUIJAZMY54R7GKKCTZX45EVAVCNFSM6AAAAABOFHNI56VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRWHE4DIMJYGA . You are receiving this because you commented.Message ID: @.***>

whatacold avatar Sep 22 '24 23:09 whatacold