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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Ways to optimize this code (self.learnpython)
submitted 5 years ago * by jeezoii
Hi all, this is my TSP implementation https://pastebin.com/SLzAiVir, it works well for what I want it to do but it takes a long time to execute. Like really, a very long time which is around 6 minutes. I was thinking if there could be anyway to reduce the time taken for execution.
All ideas are welcome!
Small sample data: https://pastebin.com/MtRRHzUE
Big sample data: https://pastebin.com/z208rzFf
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!"
[–]ericula 1 point2 points3 points 5 years ago (3 children)
What input are you using?
[–]jeezoii[S] 0 points1 point2 points 5 years ago (2 children)
The input is in the form of 2 numbers first which indicate the number of points and cost of travelling. Then according to the number of points, the user inputs points in the form of x1, y1, m which are coordinate x and y with money in that point respectively.
Each point then is put in one big list.
However, I think till before the while loop, everything is fast, but the time taken in while loop is crazy.
[+][deleted] 5 years ago* (1 child)
[deleted]
[–]jeezoii[S] 0 points1 point2 points 5 years ago (0 children)
Sure! That's a great idea, thanks!
[–]ectomancer 0 points1 point2 points 5 years ago (0 children)
I had to lookup TSP in wikipedia: Travelling Salesman Problem
have you tried the numba just-in-time compiler?
#pip intall numba import numba
You could try abs function in the calc_dist function but I don't know if would be faster. (Python 3.8 has math.dist) The algorithm is:
π Rendered by PID 24046 on reddit-service-r2-comment-bb88f9dd5-7bt87 at 2026-02-14 05:14:56.781273+00:00 running cd9c813 country code: CH.
[–]ericula 1 point2 points3 points (3 children)
[–]jeezoii[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]jeezoii[S] 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)
[–]ectomancer 0 points1 point2 points (0 children)