Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101036 > unrolled thread

Stupid Python tricks

Started bySteven D'Aprano <steve@pearwood.info>
First post2015-12-31 14:51 +1100
Last post2016-01-02 00:22 +0200
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Stupid Python tricks Steven D'Aprano <steve@pearwood.info> - 2015-12-31 14:51 +1100
    Re: Stupid Python tricks Rick Johnson <rantingrickjohnson@gmail.com> - 2015-12-30 21:02 -0800
      Re: Stupid Python tricks Steven D'Aprano <steve@pearwood.info> - 2015-12-31 17:09 +1100
        Re: Stupid Python tricks paul.hermeneutic@gmail.com - 2016-01-01 12:00 -0700
        Re: Stupid Python tricks Serhiy Storchaka <storchaka@gmail.com> - 2016-01-02 00:16 +0200
    Re: Stupid Python tricks wxjmfauth@gmail.com - 2015-12-31 03:43 -0800
    Re: Stupid Python tricks Serhiy Storchaka <storchaka@gmail.com> - 2016-01-02 00:22 +0200

#101036 — Stupid Python tricks

FromSteven D'Aprano <steve@pearwood.info>
Date2015-12-31 14:51 +1100
SubjectStupid Python tricks
Message-ID<5684a63b$0$1595$c3e8da3$5496439d@news.astraweb.com>
Stolen^W Inspired from a post by Tim Peters back in 2001:

https://mail.python.org/pipermail/python-dev/2001-January/011911.html


Suppose you have a huge string, and you want to quote it. Here's the obvious
way:

mystring = "spam"*100000
result = '"' + mystring + '"'


But that potentially involves a lot of copying. How fast is it? Using
Jython2.5, I get these results on my computer:

jy> from timeit import Timer
jy> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.4110000133514404



Perhaps % interpolation is faster?

jy> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.9660000801086426



Ouch, that's actually worse. But now we have the Stupid Python Trick:
result = mystring.join('""')

How fast is this?

jy> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
jy> min(t.repeat(number=1000))
2.171999931335449


That's Jython, which is not known for its speed. (If you want speed in
Jython, you ought to be calling Java libraries.) Here are some results
using Python 3.3:

py> from timeit import Timer
py> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.22504080459475517



Using % interpolation and the format method:

py> t = Timer("""'"{}"'.format(mystring)""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.4634905573911965
py> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.474040764849633



And the Stupid Python Trick:

py> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
py> min(t.repeat(number=1000))
0.19407050590962172


Fifteen years later, and Tim Peters' Stupid Python Trick is still the
undisputed champion!


-- 
Steven

[toc] | [next] | [standalone]


#101040

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2015-12-30 21:02 -0800
Message-ID<e56203f3-1a0f-45b6-8082-d347d311d0a1@googlegroups.com>
In reply to#101036
On Wednesday, December 30, 2015 at 9:51:48 PM UTC-6, Steven D'Aprano wrote:
> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
> undisputed champion!

And should we be happy about that revelation, or sad?

[toc] | [prev] | [next] | [standalone]


#101043

FromSteven D'Aprano <steve@pearwood.info>
Date2015-12-31 17:09 +1100
Message-ID<5684c69a$0$1589$c3e8da3$5496439d@news.astraweb.com>
In reply to#101040
On Thu, 31 Dec 2015 04:02 pm, Rick Johnson wrote:

> On Wednesday, December 30, 2015 at 9:51:48 PM UTC-6, Steven D'Aprano
> wrote:
>> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
>> undisputed champion!
> 
> And should we be happy about that revelation, or sad?

Yes!



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#101108

Frompaul.hermeneutic@gmail.com
Date2016-01-01 12:00 -0700
Message-ID<mailman.140.1451674858.11925.python-list@python.org>
In reply to#101043
On Wed, Dec 30, 2015 at 11:09 PM, Steven D'Aprano <steve@pearwood.info>
wrote:

> On Thu, 31 Dec 2015 04:02 pm, Rick Johnson wrote:
>
> >> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
> >> undisputed champion!
> >
> > And should we be happy about that revelation, or sad?
>
> Yes!
>
>
Which one, Steve? Happy or sad?

[toc] | [prev] | [next] | [standalone]


#101124

FromSerhiy Storchaka <storchaka@gmail.com>
Date2016-01-02 00:16 +0200
Message-ID<mailman.152.1451686612.11925.python-list@python.org>
In reply to#101043
On 01.01.16 21:00, paul.hermeneutic@gmail.com wrote:
> On Wed, Dec 30, 2015 at 11:09 PM, Steven D'Aprano <steve@pearwood.info>
> wrote:
>
>> On Thu, 31 Dec 2015 04:02 pm, Rick Johnson wrote:
>>
>>>> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
>>>> undisputed champion!
>>>
>>> And should we be happy about that revelation, or sad?
>>
>> Yes!
>>
>>
> Which one, Steve? Happy or sad?

True.

[toc] | [prev] | [next] | [standalone]


#101050

Fromwxjmfauth@gmail.com
Date2015-12-31 03:43 -0800
Message-ID<9543d6f0-14db-497b-bc66-c8911e159e60@googlegroups.com>
In reply to#101036
Le jeudi 31 décembre 2015 04:51:48 UTC+1, Steven D'Aprano a écrit :
> Stolen^W Inspired from a post by Tim Peters back in 2001:
> 
> https://mail.python.org/pipermail/python-dev/2001-January/011911.html
> 
> 
> Suppose you have a huge string, and you want to quote it. Here's the obvious
> way:
> 
> mystring = "spam"*100000
> result = '"' + mystring + '"'
> 
> 
> But that potentially involves a lot of copying. How fast is it? Using
> Jython2.5, I get these results on my computer:
> 
> jy> from timeit import Timer
> jy> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
> jy> min(t.repeat(number=1000))
> 2.4110000133514404
> 
> 
> 
> Perhaps % interpolation is faster?
> 
> jy> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
> jy> min(t.repeat(number=1000))
> 2.9660000801086426
> 
> 
> 
> Ouch, that's actually worse. But now we have the Stupid Python Trick:
> result = mystring.join('""')
> 
> How fast is this?
> 
> jy> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
> jy> min(t.repeat(number=1000))
> 2.171999931335449
> 
> 
> That's Jython, which is not known for its speed. (If you want speed in
> Jython, you ought to be calling Java libraries.) Here are some results
> using Python 3.3:
> 
> py> from timeit import Timer
> py> t = Timer("""'"' + mystring + '"'""", 'mystring = "spam"*100000')
> py> min(t.repeat(number=1000))
> 0.22504080459475517
> 
> 
> 
> Using % interpolation and the format method:
> 
> py> t = Timer("""'"{}"'.format(mystring)""", 'mystring = "spam"*100000')
> py> min(t.repeat(number=1000))
> 0.4634905573911965
> py> t = Timer("""'"%s"' % mystring""", 'mystring = "spam"*100000')
> py> min(t.repeat(number=1000))
> 0.474040764849633
> 
> 
> 
> And the Stupid Python Trick:
> 
> py> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
> py> min(t.repeat(number=1000))
> 0.19407050590962172
> 
> 
> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
> undisputed champion!
> 
> 
> -- 
> Steven

Yes, it's brillant.

>>> sys.version
'3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit 
(Intel)]'
>>> t = Timer("""mystring.join('""')""", 'mystring = "spam"*100000')
>>> t.repeat(number=10000)
[0.9856804212181487, 0.9692183602842306, 0.968442104343012]
>>> t = Timer("""mystring.join('\U00010000\U00010000')""", 'mystring = "spam"*100000')
>>> t = Timer("""mystring.join('\U00010000\U00010000')""", 'mystring = "spam"*100000')
>>> t.repeat(number=10000)
[15.017127648220367, 14.957529229718602, 15.173767117703846]
>>>

It is not a surprise a Python 3.3+ application runs
1000 times slower than the equivalent Python 3.2
application [*].

[*] When it does not become buggy.

jmf

[toc] | [prev] | [next] | [standalone]


#101125

FromSerhiy Storchaka <storchaka@gmail.com>
Date2016-01-02 00:22 +0200
Message-ID<mailman.153.1451686946.11925.python-list@python.org>
In reply to#101036
On 31.12.15 05:51, Steven D'Aprano wrote:
> Fifteen years later, and Tim Peters' Stupid Python Trick is still the
> undisputed champion!

It may be platform depended, but on my computer the obvious way is 10% 
faster the Stupid Python Trick.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web