modular exponentiation?
|
06-25-2018, 04:31 PM
(This post was last modified: 06-25-2018 04:44 PM by Thomas Klemm.)
Post: #21
|
|||
|
|||
RE: modular exponentiation?
Finally a solution for Java that might be accepted:
Map Reduce Code: import java.math.BigInteger; Parallel Streams With this small change we can run the calculation in parallel: Code: import java.math.BigInteger; Will it be faster on a MacBook with 4 cores? Guess what: it makes it even a bit slower. These are the results for the real time of 20 runs: Sequential Code: 0.2100 # Parallel Code: 0.2300 # We have to increase the upper limit to 10000000 to notice the difference: Sequential Code: real 0m35.134s Parallel Code: real 0m17.546s During this run the CPU was used up to 350% by this program: Code: PID USER PRI NI VIRT RES S CPU% MEM% TIME+ Command Premature Optimization Quote:Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered.-- Donald Knuth My Python program above runs in about 0.045s. It's not1 optimised but doing so wouldn't gain that much. It took me much longer to write the Java program. And today the biggest cost is our time. Not only the time that you spend to implement a piece of code. But also the time you or others must spend debugging and maintaining your code. Contrary to what others might think I consider this a reasonable interview question. Because it could lead the discussion into different directions. [1]: Ok, it's a bit optimized in that instead of a list-comprehension (using []) the corresponding generator (using ()) is used. Thus not the whole list has to be kept im memory. Instead summation and producing the arguments can be interleaved. But even this doesn't change much. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 5 Guest(s)