1 | import timeit
|
---|
2 | import decimal
|
---|
3 | from collections import OrderedDict
|
---|
4 |
|
---|
5 | import django
|
---|
6 | from django.conf import settings
|
---|
7 | from django.utils.numberformat import format
|
---|
8 |
|
---|
9 | settings.configure(USE_L10N=True, LANGUAGE_CODE='en', USE_THOUSAND_SEPARATOR=False)
|
---|
10 | django.setup()
|
---|
11 |
|
---|
12 |
|
---|
13 | def do_bench():
|
---|
14 | for bench, args in OrderedDict((
|
---|
15 | ('Small number, no decimal_pos, no grouping', (
|
---|
16 | (100, '.', None, 0, '', False),
|
---|
17 | (100.5, '.', None, 0, '', False),
|
---|
18 | (decimal.Decimal('100.5'), '.', None, 0, '', False),
|
---|
19 | ('100', '.', None, 0, '', False),
|
---|
20 | ('100.5', '.', None, 0, '', False),
|
---|
21 | )),
|
---|
22 |
|
---|
23 | ('Small number, with decimal_pos, no grouping', (
|
---|
24 | (10, '.', 2, 0, '', ),
|
---|
25 | (10.888, '.', 2, 0, '', ),
|
---|
26 | (decimal.Decimal('10.888'), '.', 2, 0, '', ),
|
---|
27 | ('10', '.', 2, 0, '', ),
|
---|
28 | ('10.888', '.', 2, 0, '', ),
|
---|
29 | )),
|
---|
30 |
|
---|
31 | ('Small number, no decimal_pos, with grouping', (
|
---|
32 | (100, '.', None, 3, ' ', True),
|
---|
33 | (100.0123456789, '.', None, 3, ' ', True),
|
---|
34 | (decimal.Decimal('-100.0123456789'), '.', None, 3, ' ', True),
|
---|
35 | ('-100', '.', None, 3, ' ', True),
|
---|
36 | ('-100.0123456789', '.', None, 3, ' ', True),
|
---|
37 | )),
|
---|
38 |
|
---|
39 | ('No decimal_pos, no grouping', (
|
---|
40 | (-1234567890, '.', None, 0, '', False),
|
---|
41 | (-1234567890.0123456789, '.', None, 0, '', False),
|
---|
42 | (decimal.Decimal('-1234567890.0123456789'), '.', None, 0, '', False),
|
---|
43 | ('-1234567890', '.', None, 0, '', False),
|
---|
44 | ('-1234567890.0123456789', '.', None, 0, '', False),
|
---|
45 | )),
|
---|
46 |
|
---|
47 | ('No decimal_pos, with grouping', (
|
---|
48 | (-1234567890, '.', None, 3, ' ', True),
|
---|
49 | (-1234567890.0123456789, '.', None, 3, ' ', True),
|
---|
50 | (decimal.Decimal('-1234567890.0123456789'), '.', None, 3, ' ', True),
|
---|
51 | ('-1234567890', '.', None, 3, ' ', True),
|
---|
52 | ('-1234567890.0123456789', '.', None, 3, ' ', True),
|
---|
53 | )),
|
---|
54 |
|
---|
55 | ('With decimal_pos, with grouping', (
|
---|
56 | (-1234567890, '.', 2, 3, ' ', True),
|
---|
57 | (-1234567890.0123456789, '.', 2, 3, ' '),
|
---|
58 | (decimal.Decimal('-1234567890.0123456789'), '.', 2, 3, ' ', True),
|
---|
59 | ('-1234567890', '.', 2, 3, ' ', True),
|
---|
60 | ('-1234567890.0123456789', '.', 2, 3, ' ', True),
|
---|
61 | )),
|
---|
62 | )).items():
|
---|
63 | print(bench)
|
---|
64 | for args in args:
|
---|
65 | print('format{!r} -> {}'.format(args, format(*args)))
|
---|
66 | print(timeit.repeat('format{!r}'.format(args), setup='from __main__ import format; from decimal import Decimal', number=500000))
|
---|
67 | print('')
|
---|
68 |
|
---|
69 |
|
---|
70 | if __name__ == '__main__':
|
---|
71 | do_bench()
|
---|