cython boundscheck=True faster than boundscheck=False

Solution 1:

Boundscheck is a security check that you are accessing indices inside the bounds of the vectors. If you don't bother to do the check if the indices can go out of bounds then it is faster. It takes time to perform the check.

That is, if boundcheck is true, it will check to see if the index is inside the range of the vector before reading or writing to memory. And if not it will throw an error. If boundcheck is false, it will read or write to the pointer even if the index is out of bounds, given out false data by reading and writing to memory corrupting data by writing.

From documentation:

The array lookups are still slowed down by two factors:

1) Bounds checking is performed.

2) Negative indices are checked for and handled correctly.

The consequences of not bound checking being:

Now bounds checking is not performed (and, as a side-effect, if you ‘’do’’ happen to access out of bounds you will in the best case crash your program and in the worst case corrupt data).

Where this is particularly important is you can have None vectors. Here is the warning from the documentation:

Warning

Speed comes with some cost. Especially it can be dangerous to set typed objects (like f, g and h in our sample code) to None. Setting such objects to None is entirely legal, but all you can do with them is check whether they are None. All other use (attribute lookup or indexing) can potentially segfault or corrupt data (rather than raising exceptions as they would in Python).

The actual rules are a bit more complicated but the main message is clear: Do not use typed objects without knowing that they are not set to None.

http://docs.cython.org/src/userguide/numpy_tutorial.html