Speed of str(positive_integer)..

alejandro david weil aweil at mail.ru
Tue Jun 29 00:58:38 CEST 2004


I asked myself how much slower would be an integer-positive number to 
string convertion function against the default str().

And this function:  
  
def myconv(number):
    if number==0:
        return '0'
    s = ''
    l = dig
    while number > 0:
        n = number//10
        t = number-((n*10))
        s = l[t]+s
        number = n
    return s

throw these results (using Psyco):

Testing 10000 times with numbers upto: 100
------------------------------------------
str(object) -> string                    -> 1.39086294174
Simplest for integer>0 (myconv)          -> 0.463662147522
This should be faster but don't          -> 1.7972369194

Testing numbers upto 1000000
----------------------------
str(object) -> string                    -> 1.48166298866
Simplest for integer>0 (myconv)          -> 1.89623999596
This should be faster but don't          -> 6.55253386497


Notes:

* It was faster for little numbers, and it seems that with
numbers growing gets worse.

*Without psyco, my native myconv is slower than str().

*Obviously, a simple list with the first 256 mixed with
str() (as I propoused on the previous thread :-) breaks all
the stats! (With and without psyco..)

*The thrid test is using divmod() that i thought that would be
faster (i thought it could make only one division and get both
qoutient and remainder, without having to multiply..) but
was worse.

*Also tried that function using lists instead of strings, 
but gaves worse results.

I include the source (with all the functions), so you can play with it. :-)

See ya,
alejandro

PS: I want a faster divmod! :-)
-- 
+ There is no dark side of the moon really. Matter of fact it's all dark.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test_strconv.py
Type: text/x-python
Size: 2077 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040628/71ecb3f6/attachment.py>


More information about the Python-list mailing list