numpy/numpy — pull request #3567 by arinkverma (2013-08-02 23:57:56+00:00)
Title: ENH: Avoiding conversion of integer to NumPy Scalar.

There is a bottleneck in scalar operations, when trying to extract the underlying C value from a Python integer.
Numpy converts the Python scalar into its matching scalar (e.g. PyLong -> int32) and then it extracts the C value from the NumPy scalar.

For float, Raul's has already made some enhancements (#2941). Based on that I am doing it for Integers.

Performance
------------------
1.09 Array * Array
1.09 Array + Array
1.18 np.int * np.int
1.14 np.int + np.int
1.08 PyInt * Array
1.07 np.int * Array
1.10 PyInt + Array
1.09 np.int + Array
1.08 PyInt * PyInt
1.09 PyInt + PyInt
2.53 PyInt * np.int
2.69 PyInt + np.int
1.06 np.int * np.int
1.13 np.int + np.int
7.29 PyInt < np.int
2.38 np.int ** 2
1.07 PyInt ** 2

where

Array = np.array([2234,32342])
PyType = 3
NyType = Array[0]

Speedup Datasheet is https://docs.google.com/spreadsheet/ccc?key=0AnPqyp8kuQw0dHpITGFYWlVZelhHSl83bzA5U0dlMmc&usp=sharing

Test case
---------------------
I have written test cases to ensure the behavior of integer as it is.

Example:
For np.int16

a = np.array([2**15-1]], dtype=np.int16)
b = np.array([-2**15], dtype=np.int16)
assert_equal(a,b-1)

For np.uint16

a = np.array([2**16-1]], dtype=np.uint16)
b = np.array([0], dtype=np.uint16)
assert_equal(a+1,b)