When dealing with big numbers (over 100,000 digits). I find str(myint) too slow. The code below takes my machine 8 seconds. Doing a quick test in java takes 0.186 seconds

import time
#**setup part**
i = 0
myint = 1
while i < 100000:
    i = i + 1
    myint = myint * 10
#** end setup section**

#****** start relevant section ******
startTime = time.time()
mystring = str(myint)
#****** end relevant section   ******

some of the operations I need to do require a string, other operations I need to do is addition, which requires a int. When dealing with huge numbers > 100,000, what can be done to make it faster?

Perhaps you could download a library for big numbers like gmpy and compare the speeds with python's big ints ?

You could try cypes and convert via C, which should be a lot faster. Also note the surprising difference when using the decimal class below.

import decimal
import datetime

#**setup part**
j = 0
myint = 1
while j < 100000:
    j = j + 1
    myint = myint * 10
#** end setup section**

#****** start relevant section ******
start_time = datetime.datetime.now()
mystring = str(myint)
print "str =", datetime.datetime.now() - start_time

start_time = datetime.datetime.now()
mystring = "%s" %(myint)
print "% s =", datetime.datetime.now() - start_time
#****** end relevant section   ******


j = 0
myint = decimal.Decimal(1)
while j < 100000:
    j += 1
    myint = myint * decimal.Decimal(10)
start_time = datetime.datetime.now()
mystring = "%s" % (myint)
print "decimal =", datetime.datetime.now() - start_time

# -----------------------------------------------------------------------
# my results 
# str =     0:00:06.853879
# % s =     0:00:06.828796
# decimal = 0:00:00.000054 (if you are using MS Windows, this
#                              will come out as zero because Windows
#                              only goes down to milli-seconds)

You could try cypes and convert via C, which should be a lot faster. Also note the surprising difference when using the decimal class below.[

The string returned in the decimal case is 1.000000000000000000000000000E+100000 . It has only 37 characters and not 100001.

gmpy has an excellent score:

[t212818] python woooee.py
str = 0:00:03.986846
% s = 0:00:04.035936
decimal = 0:00:00.000076
gmpy = 0:00:00.013480

The code added is

import gmpy
myint = gmpy.mpz(10**100000)
start_time = datetime.datetime.now()
mystring = "%s" % (myint)
print "gmpy =", datetime.datetime.now() - start_time

So I suggest this alternative to str:

def str_int(myint):
    "same as str(myint) but faster"
    return str(gmpy.mpz(myint))

It has only 37 characters and not 100001.

Of course it does as that is the default. Changing to 110,000 significant digits meant that it took as long to convert to decimal as it did to convert to a string (of course). Oh well.

When dealing with big numbers (over 100,000 digits). I find str(myint) too slow. The code below takes my machine 8 seconds. Doing a quick test in java takes 0.186 seconds

import time
#**setup part**
i = 0
myint = 1
while i < 100000:
    i = i + 1
    myint = myint * 10
#** end setup section**

#****** start relevant section ******
startTime = time.time()
mystring = str(myint)
#****** end relevant section   ******

some of the operations I need to do require a string, other operations I need to do is addition, which requires a int. When dealing with huge numbers > 100,000, what can be done to make it faster?

If Java is sooo much faster, why do you bother with Python?

My question is, can Java handle the factorial of 25500 accurately? This factorial is 101295 digits long? Java may just handle your very large integer as a scientific notation floating point number internally. Python handles it as an accurate string internally.

If Java is sooo much faster, why do you bother with Python?

I am learning python, sort of hard to learn python by using java. I am working on a set of programming puzzles found at spoj.pl. Working on these tough (for me) puzzles helps me learn the ins and outs of python that I would never learn by simply reading a book.

I am learning python, sort of hard to learn python by using java. I am working on a set of programming puzzles found at spoj.pl. Working on these tough (for me) puzzles helps me learn the ins and outs of python that I would never learn by simply reading a book.

Thanks for letting us know!

Do us a favor and calculate the factorial of 25500 in Python and in Java. Convert both results to strings and compare the 2 strings for accuracy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.