What is the performance of these?
BigInteger -> toString() // what is the runtime?
String -> toCharArray() // what is the runtime?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Conversion of
BigIntegerto string isO(N^2), whereNis the number of digits in the result, when the base of the internal representation does not divide the target base; when the target base is divisible by the storage base, conversion takesO(N).Consider conversion to base 10 when the internal representation is base-256. A division by ten has to happen
Ntimes; each time, all elements of theBigIntegerrepresentation get modified. The number of elements in the representation is proportional to the number of digits in the printout, so the overall conversion takesO(N^2).On the other hand, converting to hex of a big int in base-256 internal representation takes
O(N), because division is not necessary in this case. Each subelement can be converted in isolation from the remaining ones, and the number of sub-elements is proportional to the number of digits in the printout.As far as
String.toCharArray()goes, it’sO(N), whereNis the number of characters in the string, because each character must be copied into the output.