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


Groups > comp.lang.python > #45715

Re: PEP 378: Format Specifier for Thousands Separator

Newsgroups comp.lang.python
Date 2013-05-22 00:14 -0700
References (7 earlier) <mailman.1939.1369167746.3114.python-list@python.org>,> <519c30b0$0$6599$c3e8da3$5496439d@news.astraweb.com>,> <mailman.1946.1369191420.3114.python-list@python.org>,> <519c36c6$0$6599$c3e8da3$5496439d@news.astraweb.com> <mailman.1951.1369193993.3114.python-list@python.org>
Message-ID <087e4cab-92bd-4a14-aab4-a986e426302b@googlegroups.com> (permalink)
Subject Re: PEP 378: Format Specifier for Thousands Separator
From 88888 Dihedral <dihedral88888@gmail.com>

Show all headers | View raw


Carlos Nepomuceno於 2013年5月22日星期三UTC+8上午11時38分45秒寫道:
> ----------------------------------------
> > From: steve+comp.lang.python@pearwood.info
> > Subject: Re: PEP 378: Format Specifier for Thousands Separator
> > Date: Wed, 22 May 2013 03:08:54 +0000
> > To: python-list@python.org
> [...]
> >> So, the only alternative to have "'%,d' % x" rendering the thousands
> >> separator output would a C source code modification?
> >
> > That's one alternative. But the language you would be then running will
> > no longer be Python.
> >
> > Another alternative would be to write a pre-processor that parses your
> > Python source code, extracts any reference to the above, and replaces it
> > with a call to the appropriate format call. But not only is that a lot of
> > work for very little gain, but it's also more or less impossible to do in
> > full generality. And again, what you are running will be something
> > different than Python, it will be Python plus a pre-processor.
> >
> >
> > Don't fight the language. You will lose.
> 
> Not fighting the language. In fact it's not even a language issue.
> All I need is a standard library[1] improvement: "%,d"! That's all!
> 
> Just to put in perspective the performance difference of str.__mod__() and str.format():
> 
> C:\Python27>python -m timeit -cv -n10000000 "'%d'%12345"
> raw times: 0.386 0.38 0.373
> 10000000 loops, best of 3: 0.0373 usec per loop
> 
> C:\Python27>python -m timeit -cv -n10000000 "'{:d}'.format(12345)"
> raw times: 7.91 7.89 7.98
> 10000000 loops, best of 3: 0.789 usec per loop
> 
> C:\Python27>python -m timeit -cv -n10000000 "'{:,d}'.format(12345)"
> raw times: 8.7 8.67 8.78
> 10000000 loops, best of 3: 0.867 usec per loop
> 
> That shows str.format() is 20 times slower than str.__mod__() for a simple decimal integer literal formatting.
> And it's additionally 10% slower if the thousands separator format specifier (',') is used.
> 
> [1] I think that translates to Python source code in 'Objects/stringobject.c' and maybe 'Objects/unicodeobject.c'
> 
> >
> >
> > --
> > Steven
> > --
> > http://mail.python.org/mailman/listinfo/python-list

The conversions of the  32 bit integers and 64 bit floats
 to the strings of the  base 10 digits require an 
efficint div and mod normally in the low level.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

RE: PEP 378: Format Specifier for Thousands Separator Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-21 23:22 +0300
  Re: PEP 378: Format Specifier for Thousands Separator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-22 02:42 +0000
    RE: PEP 378: Format Specifier for Thousands Separator Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-22 05:56 +0300
      Re: PEP 378: Format Specifier for Thousands Separator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-22 03:08 +0000
        RE: PEP 378: Format Specifier for Thousands Separator Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-22 06:38 +0300
          Re: PEP 378: Format Specifier for Thousands Separator 88888 Dihedral <dihedral88888@gmail.com> - 2013-05-22 00:14 -0700
        Re: PEP 378: Format Specifier for Thousands Separator Ned Batchelder <ned@nedbatchelder.com> - 2013-05-22 07:25 -0400
        RE: PEP 378: Format Specifier for Thousands Separator Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-22 14:52 +0300

csiph-web