tommikaikkonen / prettyprinter

Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
https://prettyprinter.readthedocs.io
MIT License
336 stars 20 forks source link

Running time is exponential in the depth of the structure #28

Closed Vlad-Shcherbina closed 5 years ago

Vlad-Shcherbina commented 5 years ago

Maybe that's the price of nice formatting, but just in case it's unintentional, here is how to reproduce:

import sys
import time
import prettyprinter

print(sys.version)
print(sys.platform)
print('prettyprinter', prettyprinter.__version__)

data = []
for depth in range(20):
    start = time.time()
    prettyprinter.pprint(data)
    print('it took', time.time() - start)
    data = [data]
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
win32
prettyprinter 0.14.0
[]
it took 0.000499725341796875
[[]]
it took 0.000499725341796875
[[[]]]
it took 0.0004999637603759766
[[[[]]]]
it took 0.0004992485046386719
[[[[[]]]]]
it took 0.0009992122650146484
[[[[[[]]]]]]
it took 0.0019979476928710938
[[[[[[[]]]]]]]
it took 0.0034966468811035156
[[[[[[[[]]]]]]]]
it took 0.007492542266845703
[[[[[[[[[]]]]]]]]]
it took 0.012986421585083008
[[[[[[[[[[]]]]]]]]]]
it took 0.02747201919555664
[[[[[[[[[[[]]]]]]]]]]]
it took 0.05194687843322754
[[[[[[[[[[[[]]]]]]]]]]]]
it took 0.06443476676940918
[[[[[[[[[[[[[]]]]]]]]]]]]]
it took 0.12087607383728027
[[[[[[[[[[[[[[]]]]]]]]]]]]]]
it took 0.25823545455932617
[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]
it took 0.4964914321899414
[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]
it took 0.9939820766448975
[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]
it took 2.0583908557891846
[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]
it took 4.385506868362427
[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]
it took 8.482808828353882
[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]
it took 16.102501392364502
you got the idea
tommikaikkonen commented 5 years ago

Thanks for reporting this. The fix for this was literally a one line change :)

This will speed up the library a lot for people printing highly nested data structures! Good stuff. I'll craft a release soon.

tommikaikkonen commented 5 years ago

The fix for this is released in v0.15.0.