Ticket #25762: bench_numberformat.py

File bench_numberformat.py, 2.6 KB (added by Jaap Roes, 8 years ago)

Benchmark for numberformat.format

Line 
1import timeit
2import decimal
3from collections import OrderedDict
4
5import django
6from django.conf import settings
7from django.utils.numberformat import format
8
9settings.configure(USE_L10N=True, LANGUAGE_CODE='en', USE_THOUSAND_SEPARATOR=False)
10django.setup()
11
12
13def 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
70if __name__ == '__main__':
71    do_bench()
Back to Top