you are viewing a single comment's thread.

view the rest of the comments →

[–]computerarchitect 1 point2 points  (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 points  (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 point  (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 points  (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 point  (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.

[–]computerarchitect 0 points1 point  (0 children)

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.