Skip to main content
Code Review

Return to Question

Post Reopened by 200_success, Ludisposed, Pimgd, Gerrit0, Community Bot
Post Closed as "Not suitable for this site" by Toby Speight, IEatBagels, Sᴀᴍ Onᴇᴌᴀ , alecxe, Dan Oberlam
deleted 19 characters in body; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

Advent of Code 2018 Day 1 in OCaml: Find length of cycles when accumulating deltas

I just started Advent of Code 2018 Advent of Code 2018. This is exercise 2 of day 1exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).

Basically, it accumulates integers from a provided list of ints, named input. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:

 let main () =
 let rec my_slow_code lst history =
 if lst == [] then my_slow_code input history
 else
 let current = List.hd lst + List.hd history in
 if List.mem current history then current
 else my_slow_code (List.tl lst) (current :: history)
 in
 my_slow_code input [0]

I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !

Advent of Code OCaml

I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).

Basically, it accumulates integers from a provided list of ints, named input. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:

 let main () =
 let rec my_slow_code lst history =
 if lst == [] then my_slow_code input history
 else
 let current = List.hd lst + List.hd history in
 if List.mem current history then current
 else my_slow_code (List.tl lst) (current :: history)
 in
 my_slow_code input [0]

I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !

Advent of Code 2018 Day 1 in OCaml: Find length of cycles when accumulating deltas

I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).

Basically, it accumulates integers from a provided list of ints, named input. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:

 let main () =
 let rec my_slow_code lst history =
 if lst == [] then my_slow_code input history
 else
 let current = List.hd lst + List.hd history in
 if List.mem current history then current
 else my_slow_code (List.tl lst) (current :: history)
 in
 my_slow_code input [0]

I can't understand what mistake here makes such a slow code, maybe the use of List.mem?

edited tags; edited title
Link
Ludisposed
  • 11.8k
  • 2
  • 41
  • 91

Super slow Advent of Code OCaml code

Source Link

Super slow OCaml code

I just started Advent of Code 2018. This is exercise 2 of day 1, and I can't understand why my code is so slow (4 minutes 37 seconds, compared to 200ms for a friend who did it in Clojure with the same computer).

Basically, it accumulates integers from a provided list of ints, named input. Whenever the current value of the accumulator is equal to one of its previous values, it is returned. We might have to loop many times over the provided list before we find such an occurrence. Here is the code:

 let main () =
 let rec my_slow_code lst history =
 if lst == [] then my_slow_code input history
 else
 let current = List.hd lst + List.hd history in
 if List.mem current history then current
 else my_slow_code (List.tl lst) (current :: history)
 in
 my_slow_code input [0]

I can't understand what mistake here makes such a slow code, maybe the use of List.mem? I would appreciate any insight from OCaml practitioners. Thanks a lot !

lang-ml

AltStyle によって変換されたページ (->オリジナル) /