473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

str(bigint) is slow

does anyone know how to make this faster? it seems that str(x) is the slow part.

def foo(): .... t1 = time.time()
.... x = 19 ** 314159
.... t2 = time.time()
.... y = str(x)
.... t3 = time.time()
.... print y
.... t4 = time.time()
.... print t2-t1, t3-t2, t4-t3
.... foo()

<snip a print out of a billion numbers>
3.78499984741 230.490999937 0.0700001716614
thanks,

bryan
Jul 18 '05 #1
8 4815
Bryan <be*****@yahoo.com> wrote:

does anyone know how to make this faster? it seems that str(x) is the slow part.


Do you understand how much arithmetic is required to do this? You're
talking about more than 400,000 division/modulo operations on a multi-byte
number that is 166,000 bytes long. I, for one, am not surprised that it
takes a long time.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #2
In article <tm********************************@4ax.com>,
Tim Roberts <ti**@probo.com> wrote:
Bryan <be*****@yahoo.com> wrote:

does anyone know how to make this faster? it seems that str(x) is the slow
part.


Do you understand how much arithmetic is required to do this? You're
talking about more than 400,000 division/modulo operations on a multi-byte
number that is 166,000 bytes long. I, for one, am not surprised that it
takes a long time.


It doesn't have to be that slow -- you could do a single div/mod by
10**(n/2) (n=#digits of input) to split it into two numbers of roughly
half the number of digits, and continue recursively in both halves. The
bottleneck would be the first div/mod -- Python currently uses Karatsuba
for that, right?

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #3
[David Eppstein]
It doesn't have to be that slow -- you could do a single div/mod by
10**(n/2) (n=#digits of input) to split it into two numbers of roughly
half the number of digits, and continue recursively in both halves. The
bottleneck would be the first div/mod -- Python currently uses Karatsuba
for that, right?


Computing the power in a decimal-friendly base to begin with runs much
faster than that (and I posted code the last time this came up, a
couple of weeks ago).

Python uses Karatsuba for multiplication, not division. Karatsuba may
or may not trigger during the computation of 10**(n/2) (depending on
how large n is), but never triggers during division.
Jul 18 '05 #4
tim,

your code is incredibly fast... thank you

bryan
Tim Peters wrote:
[David Eppstein]
It doesn't have to be that slow -- you could do a single div/mod by
10**(n/2) (n=#digits of input) to split it into two numbers of roughly
half the number of digits, and continue recursively in both halves. The
bottleneck would be the first div/mod -- Python currently uses Karatsuba
for that, right?

Computing the power in a decimal-friendly base to begin with runs much
faster than that (and I posted code the last time this came up, a
couple of weeks ago).

Python uses Karatsuba for multiplication, not division. Karatsuba may
or may not trigger during the computation of 10**(n/2) (depending on
how large n is), but never triggers during division.

Jul 18 '05 #5
In article <ma*************************************@python.or g>,
Tim Peters <ti********@gmail.com> wrote:
[David Eppstein]
It doesn't have to be that slow -- you could do a single div/mod by
10**(n/2) (n=#digits of input) to split it into two numbers of roughly
half the number of digits, and continue recursively in both halves. The
bottleneck would be the first div/mod -- Python currently uses Karatsuba
for that, right?


Computing the power in a decimal-friendly base to begin with runs much
faster than that (and I posted code the last time this came up, a
couple of weeks ago).

Python uses Karatsuba for multiplication, not division. Karatsuba may
or may not trigger during the computation of 10**(n/2) (depending on
how large n is), but never triggers during division.


I wonder what the breakpoint would be for doing divisions by Newton
iteration with Karatsuba multiplication (asymptotic complexity = same as
multiplication) vs naive long division. Probably too many digits to
make it worthwhile most of the time.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #6
[David Eppstein]
I wonder what the breakpoint would be for doing divisions by Newton
iteration with Karatsuba multiplication (asymptotic complexity = same as
multiplication) vs naive long division. Probably too many digits to
make it worthwhile most of the time.


GNU's GMP developers spend their lives finding these tradeoffs, so
read the GMP docs and mailing lists for more than you can likely bear
on this,

Python's bigint implementation is aimed at 100% portability and
correctness more than speed. GMP doesn't hesitate to drop into
hyper-optimized hand-coded assembler when it helps. Partly as a
consequence of the resulting speed difference in the lowest-level
primitives, the cutoff for plain Karatsuba multiplication in Python is
so high that Karatsuba rarely triggers in Python (and probably never
in 99.99% of Python applications).

Do look at the last, recent iteration of this topic. The asymptotics
of elementary arithmetic operations are independent of base, so
computing powers in a decimal-friendly base to begin with wins big by
allowing conversion to decimal string to be done in time linear in the
number of digits. This allowed a pure-Python program to run circles
around GMP in producing the decimal representation of a very large
power.
Jul 18 '05 #7
Tim Peters wrote:
Python's bigint implementation is aimed at 100% portability and
correctness more than speed.


tim,

your BigDec class is so fast it's like night and day compared to the standard functions. are you implying that your
BigDec isn't 100% portable or correct or has some limitations? why can't python longs or bigints use your
implementation under the covers?

thanks,

bryan

Jul 18 '05 #8
[Bryan]
your BigDec class is so fast it's like night and day compared to the standard
functions.
Not so: it does arithmetic much slower than the builtin arithmetic.
The only thing it's faster at is producing a decimal string at the
very end, and that only pays when the integer is so large that the
quadratic-time nature of the builtin binary-to-decimal conversion
becomes apparent. BigDec still uses the builtin binary-to-decimal
conversion for integers up to 5000 decimal digits, which is already
larger than virtually any real app ever uses. For ints much longer
than that, BigDec conversion to decimal string takes time linear in
the number of decimal digits, which has an unboundedly growing
advantage over the quadratic-time builtin conversion as the number of
digits increases. So the only thing it's good for is printing very
large powers in decimal. If you were willing to see your very large
powers in hex (base 16) instead, the builtin "print hex(large_power)"
is again much faster than BigDec.
are you implying that your BigDec isn't 100% portable or correct or has some
limitations?
Limitations? Heck, it doesn't even implement addition. The only
things it implements are multiplication and exponentiation of positive
integers (and relying heavily on the builtin binary longs to do most
of that), and efficient conversion to decimal string. That's a tiny
subset of what builtin longs support.
why can't python longs or bigints use your implementation under the covers?


Because it would be insane <wink>. After a few years of work (if
speed-obsessed volunteers appear to do it), the new-in-2.3.4 decimal
module should approach comparable speeds. For more reasons than I
count now, trying to use the current version of the decimal module to
print large powers in decimal is slower than using builtin longs.
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Freddie | last post by:
Hi, I posted a while ago for some help with my word finder program, which is now quite a lot faster than I could manage. Thanks to all who helped :) This time, I've written a basic batch...
5
by: kramb64 | last post by:
I already submitted a bug, but I'm curious to know if anybody ran into this. Try this program on Windows with python 2.3.3 and with python 2.2: import shelve a=shelve.open("a", "c") for x in...
8
by: zorro | last post by:
I need to read the entire eventlog and parse it. The application uses vb.net and the EventLog object. The problem i'm having is that it can take less then a second up to 15 seconds to read all...
5
by: Rafał Maj Raf256 | last post by:
Hi, is there some small project that proviedes bigint, best as a C++ class, or just as set of struct/functions (C-style)? Something like GMP and it's bigint, but I want to have it just in one...
4
by: Tosha | last post by:
Is there a library to work with 128,256,512, or 1024 bit integers? Microsoft PowerToy Calculator can calculate up to 2^1024: ...
3
by: chrisperkins99 | last post by:
It seems to me that str.count is awfully slow. Is there some reason for this? Evidence: ######## str.count time test ######## import string import time import array s = string.printable *...
22
by: Albert Oppenheimer | last post by:
I thought my program had to be caught in a loop, and cancelled it through the task manager. It took about one second in Java, but re-implemented in C, it had already run over one minute. I set...
6
by: bearophileHUGS | last post by:
When I use dictionaries I find the dict.get() method very handy, and I'd like to use it often, but I think it's quite slow. This is a small benchmark: from random import randrange from timeit...
1
by: maestro | last post by:
If isMult is slow then: if len(str(a)) == len(str(r)) and isMult(a, r): trues.append((a, r)) will be much faster than: if isMult(a, r) and len(str(a)) == len(str(r)): trues.append((a, r))
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.