Speed of str(positive_integer)..

From: alejandro david weil (aweil_at_mail.ru)
Date: 06/29/04


Date: Mon, 28 Jun 2004 19:58:38 -0300
To: python-list@python.org


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.




Relevant Pages

  • RE: Speed of str(positive_integer)..
    ... > string convertion function against the default str. ... my native myconv is slower than str(). ... the timeit module is useful for doing these sorts of things. ...
    (comp.lang.python)
  • Re: String#chop slow? REALLY slow?
    ... Jake McArthur writes: ... VALUE str; ... And that's the reason why it's slower... ...
    (comp.lang.ruby)