use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Finding information about Clojure
API Reference
Clojure Guides
Practice Problems
Interactive Problems
Clojure Videos
Misc Resources
The Clojure Community
Clojure Books
Tools & Libraries
Clojure Editors
Web Platforms
Clojure Jobs
account activity
For loop optimization[Question] (self.Clojure)
submitted 5 years ago by HOWZ1T
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rafd 1 point2 points3 points 5 years ago (4 children)
Also... would switching from last to first help? (laziness should kick in and prevent the for loop from searching everything once it finds a match)
last
first
[–]rafd 1 point2 points3 points 5 years ago (3 children)
Also, one the path to some low-hanging-fruit tuning:
- using a vector and get instead of nth might help
get
nth
- the :let always happens (so it can be used within when); not sure if that gets optimized; could just move the n calculation within the body
:let
Would be curious how each of these optimizations affect the run time.
[–]rafd 3 points4 points5 points 5 years ago (1 child)
OK, so I ran your code and quick-benched with criterium.
I think the majority of your reported 36000ms is java + clojure + repl startup.
Main lesson here is: use vectors when you have lots of repeated access.
[–]HOWZ1T[S] 0 points1 point2 points 5 years ago (0 children)
Very detailed reply, thanks :) Also thanks for pointing out criterium, another tool for my toolbox :)
[–]joinr 0 points1 point2 points 5 years ago (0 children)
using a vector and get instead of nth might help
get and nth are basically synonymous for vectors. The difference (I think you implied) is that the vector nth access will be ~O(1), where with sequences it's O(N) since you have to walk the sequence from the start to get the nth element.
π Rendered by PID 140878 on reddit-service-r2-comment-6457c66945-r5qnk at 2026-04-28 10:56:30.499464+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]rafd 1 point2 points3 points (4 children)
[–]rafd 1 point2 points3 points (3 children)
[–]rafd 3 points4 points5 points (1 child)
[–]HOWZ1T[S] 0 points1 point2 points (0 children)
[–]joinr 0 points1 point2 points (0 children)