Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:38:16+00:00 2026-06-15T17:38:16+00:00

I am writing a program where time is important, and I just realized through

  • 0

I am writing a program where time is important, and I just realized through a lot of debugging prints that my big holdup (80% of computing time) is converting a very large BigInteger (50K digits) into a string.
Is this behavior to be expected or how can I change something to make it run faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:38:17+00:00Added an answer on June 15, 2026 at 5:38 pm

    Converting numbers to strings is an expensive operation even if you use long and double.

    Normally, the only thing more expensive is the IO you perform when writing the text for a file or the console.

    It is worth noting that the built in converter a number to text is an O(N^2) operation where N is the number of digits. As such it is not surprising that 50K digit numbers take a very long time to convert to a decimal String.


    Based on tmyklebu’s suggestion I have written this. It is slower for numbers with less than 500 digits, but is much faster in the range of 50,000 digits.

    public static void main(String... args) {
        BigInteger bi = BigInteger.valueOf(11).pow(48100);
        System.out.println(bi.toString());
        System.out.println(toString(bi));
        System.out.println("bi.length=" + bi.toString().length() + ", toString(bi).length=" + toString(bi).length());
        for (int i = 0; i < 10; i++) {
            long start = System.nanoTime();
            String s = bi.toString();
            long mid = System.nanoTime();
            String s2 = toString(bi);
            long end = System.nanoTime();
            System.out.printf("time1 %.3f ms, time2 %.3f ms%n", (mid - start) / 1e6, (end - mid) / 1e6);
            if (!s.equals(s2))
                throw new AssertionError();
        }
    }
    
    public static String toString(BigInteger bi) {
        StringBuilder sb = new StringBuilder();
        int i = 16;
        while (bi.compareTo(powerOfTen(i)) > 0)
            i *= 2;
        toString(bi, sb, i);
        int start = 0;
        while (sb.charAt(start) == '0')
            start++;
        return sb.substring(start);
    }
    
    private static void toString(BigInteger bi, StringBuilder sb, int digits) {
        if (digits < 18) {
            int start = sb.length();
            for (int i = 0; i < digits; i++)
                sb.append('0');
            long l = bi.longValue();
            for (int i = digits - 1; i >= 0; i--, l /= 10)
                sb.setCharAt(start + i, (char) ('0' + l % 10));
        } else {
            int digits2 = digits / 2;
            BigInteger[] parts = bi.divideAndRemainder(powerOfTen(digits2));
            toString(parts[0], sb, digits - digits2);
            toString(parts[1], sb, digits2);
        }
    }
    
    private static final Map<Integer, BigInteger> powersOfTen = new HashMap<Integer, BigInteger>();
    
    private static BigInteger powerOfTen(int digits2) {
        BigInteger tens = powersOfTen.get(digits2);
        if (tens == null)
            powersOfTen.put(digits2, tens = BigInteger.TEN.pow(digits2));
        return tens;
    }
    

    prints

    973096948397248203274473625697464617461138859359846077811290536......
    973096948397248203274473625697464617461138859359846077811290536......
    bi.length=50091, toString(bi).length=50091
    time1 525.892 ms, time2 67.260 ms
    time1 458.559 ms, time2 98.178 ms
    time1 441.275 ms, time2 92.902 ms
    time1 399.339 ms, time2 98.448 ms
    time1 518.761 ms, time2 97.804 ms
    time1 396.884 ms, time2 65.651 ms
    time1 363.945 ms, time2 98.827 ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am writing a program that is dependent on time and observing some
I am writing a program that will need to measure time in minutes. To
I am writing a program that contains a JButton . Every time the button
I'm writing a program in C++ that will run all the time in the
I am writing a program that keeps track of different transactions done over time.
I am writing a program that should search through a directory that the user
I'm writing a cross-platform Qt-based program that from time to time needs to play
I'm writing a program that needs to be able to read in the time
I am writing a program that must register time of starting a process such
Let we explain what I mean. Some time ago, while writing a program in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.