How can I quickly load a large txt file into BigInteger?

白昼怎懂夜的黑 提交于 2019-11-27 16:08:32

As an optimization, since BigInteger is Serializable, you could save it to a binary file once and speed up your loading considerably.

Loading a serialized object should be way faster than parsing a huge string everytime.

Use ObjectOutputStream to save your big integer and ObjectInputStream to read it back in.

It is slow because new BigInteger(String) is doing radix conversion from decimal to binary, which is O(N2). Nothing you can do about that.

You could save either the object itself, via Serialization, or the byte array it is stored in, via BigInteger.toByteArray(). Either will load essentially instanteously.

As the comments have indicated, your code is slow because you're attempting to load a number with a lot of digits.

If you're unsatisfied with the performance of Java's BigInteger implementation, then I suggest you look elsewhere.

This library claims to have a BigInteger that outperforms Java's implementation (note it may not speed up loading the number, but it should improve multiplication and division performance).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!