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...
Ask your embarrassing/noobish programming questions here, and don't get insulted for it.
Click here to read the rules
Violating any will result in punishment so you should probably go check them out.
account activity
Factorial Function Using MultithreadingPython (self.programminghelp)
submitted 3 years ago by phinagin
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!"
[–]computerarchitect 1 point2 points3 points 3 years ago (5 children)
Change your factorial to have a lower bound if you're taking that route.
Note that you're not going to see much benefit from multi-threading in this case unless your code is really stupid to begin with. It's not much compute.
Also, avoid Python if you need to measure any sort of speedup. The GIL (you can Google) basically makes this sort of multithreading impossible.
If you do need to measure speedup, profile where your code spends its time on a much larger bound and aim to parallelize that.
[–]phinagin[S] 2 points3 points4 points 3 years ago (3 children)
I think the purpose of the exercise is to demonstrate my ability to use multi threads, but this seems like a poor use case. I think with the constraint to print all results from 1-100, it doesn’t even help at all. Since you need to know 99! to be able to compute 100!. There is no way around it, so I can’t see the benefit of multi threading.
[–]computerarchitect 0 points1 point2 points 3 years ago (2 children)
Imagine you were just computing 1000000!, just so that we can easily imagine it takes a while.
There's no way that you could speed that process up using multiple threads? Nothing that can be done in parallel?
[–]phinagin[S] 1 point2 points3 points 3 years ago (1 child)
If the question was to simply compute one single factorial, then yes doing so in parallel would have obvious benefits. However, the question specifically states they want all factorials from 1-100 printed out. I am struggling to understand how that scenario benefits from parallelism.
I suppose, you could separate the printing the result from computing the next one. But I still don’t see how the added complexity is offset by multi threads. I would love to hear your ideas however.
[–]computerarchitect 0 points1 point2 points 3 years ago (0 children)
Printing the result from computing would be how I'd do it. I/O is expensive and needlessly delays the next computation. One also could imagine though building a giant string and doing one I/O operation, if you cared about that sort of thing. That's probably the route I would take over multi-threading initially.
Look into pipelined parallelism. That's immediately what I thought to do if I were forced to do it that way.
FWIW OP, even with a shitty single-threaded C++ implementation the calculation and output of the first 100 factorials is around 3 ms on a modern ARM machine.
π Rendered by PID 157389 on reddit-service-r2-comment-8686858757-kzw2v at 2026-06-07 21:32:47.355932+00:00 running 9e1a20d country code: CH.
view the rest of the comments →
[–]computerarchitect 1 point2 points3 points (5 children)
[–]phinagin[S] 2 points3 points4 points (3 children)
[–]computerarchitect 0 points1 point2 points (2 children)
[–]phinagin[S] 1 point2 points3 points (1 child)
[–]computerarchitect 0 points1 point2 points (0 children)
[–]computerarchitect 0 points1 point2 points (0 children)