I was working on a factorial function, and factorial with big integers can get REALLY long.
For example, 200000! = 973350 digits long and if i just use ToString(), things take a very long time.
200000! takes longer to convert to string then actually compute it!
I've tried to set the factorial function to a Process, and then use ProcessorAffinity
to pin the Thread to a specific core so that that core ONLY converts to string, but that just took exactly the same time.
Also, the reason I want to convert it to string is because I want to write the output (FactFile) to a text file.
String Conversion code:
using (Process proc = Process.GetCurrentProcess())
{
proc.ProcessorAffinity = (IntPtr)0x0003;
FactFile = Res.ToString(); //FactFile is Going to be the final string, Res is the Factorial.
}
here's my code for Factorial:
for (BigInteger i = 1; i < k; i++)
{
Res *= i; // Res is the number to Calculate the factorial of
}
200000! takes 15 seconds to compute, and then another 18 seconds to convert it to string (this can differ from cpu to cpu, I have an i7).
Reminder: What's the most efficient way to convert to string?
output:
Total Computation Time: 00:00:16.2007276
String Conversion Time: 00:00:19.4049292