Why is this imported default module far faster than its raw source

138 views Asked by At

This question arises from an answer I provided to another question (optimising a binary search for a specific purpose)

The example is the bisect library.

When I run the bisect library using the default from bisect import *, it's >3.5 times faster than its own raw source code pasted into ipython...

# define array
a = [0, 0, 0, 0, 1, 2, 3, 3, 3, 4, 5, 5, 5, 6, 6, 11, 15, 24]

# Copying the raw code from the source from bisect into ipython
# using ipython's magic %paste command
%paste
...
%timeit bisect_left(a, 1), bisect_right(a, 1)
1000000 loops, best of 3: 1.77 µs per loop

# now importing and using the python default functions...
from bisect import bisect_left, bisect_right
%timeit bisect_left(a, 1), bisect_right(a, 1)
1000000 loops, best of 3: 504 ns per loop

Question

  1. What causes this speed-up.
  2. How can I gain this speed-up with my own code?
0

There are 0 answers