isaacg1 / pyth

Pyth, an extremely concise language. Try it here:
https://pyth.herokuapp.com/
MIT License
263 stars 57 forks source link

Huge asymptotic speedup for base conversion #205

Closed andersk closed 8 years ago

andersk commented 8 years ago

Use a recursive algorithm based on repeatedly squaring the base for both to_base_ten and from_base_ten. The from_base_ten speedup is not as huge as I hoped, because Python’s divmod is pathetically slow, but it at least it’s slow C code rather than slow Python code.

Here are some benchmarks:

Old to_base_ten:
  lC*d    100000    2.6s
  lC*d    200000   10s
  lC*d    400000   42s

New to_base_ten:
  lC*d    100000    0.046s
  lC*d    200000    0.093s
  lC*d    400000    0.19s
  ⋮
  lC*d  12800000    7.5s
  lC*d  25600000   16s
  lC*d  51200000   39s

Old from_base_ten:
  lC^256   50000    4.9s
  lC^256  100000   19s
  lC^256  200000   76s

New from_base_ten:
  lC^256   50000    0.12s
  lC^256  100000    0.37s
  lC^256  200000    1.3s
  lC^256  400000    4.8s
  lC^256  800000   19s
  lC^256 1600000   76s