summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-06-27 22:08:07 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-06-27 22:08:07 +0000
commit36c72bfc2118e52160d3445e06b6e1cc17a2cba7 (patch)
tree5ab06939e7e4d7dc8fed3534f0d12a2fcc5e9e00
parent47c92e4224ee36090289fcafa6820f749bc3d9b1 (diff)
downloadnumpy-36c72bfc2118e52160d3445e06b6e1cc17a2cba7.tar.gz
Add a function to retrieve a user-defined type number from the name of the associated type-object
-rw-r--r--benchmarks/casting.py9
-rw-r--r--numpy/core/code_generators/multiarray_api_order.txt1
-rw-r--r--numpy/core/src/arrayobject.c26
3 files changed, 33 insertions, 3 deletions
diff --git a/benchmarks/casting.py b/benchmarks/casting.py
new file mode 100644
index 000000000..84558a088
--- /dev/null
+++ b/benchmarks/casting.py
@@ -0,0 +1,9 @@
+import timeit
+N = [1000,100]
+t1 = timeit.Timer('b = a.astype(int)','import numpy;a=numpy.zeros(shape=%s,dtype=float)'%N)
+t2 = timeit.Timer('b = a.astype("l")','import Numeric;a=Numeric.zeros(shape=%s,typecode="d")'%N)
+t3 = timeit.Timer("b = a.astype('l')","import numarray; a=numarray.zeros(shape=%s,typecode='d')"%N)
+print "1-D length = ", N
+print "NumPy: ", t1.repeat(3,100)
+print "Numeric: ", t2.repeat(3,100)
+print "Numarray: ", t3.repeat(3,100)
diff --git a/numpy/core/code_generators/multiarray_api_order.txt b/numpy/core/code_generators/multiarray_api_order.txt
index 67b09d190..10bd1219e 100644
--- a/numpy/core/code_generators/multiarray_api_order.txt
+++ b/numpy/core/code_generators/multiarray_api_order.txt
@@ -72,3 +72,4 @@ PyArray_RegisterCastFunc
PyArray_RegisterCanCast
PyArray_InitArrFuncs
PyArray_IntTupleFromIntp
+PyArray_TypeNumFromName
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index bd064b3f4..75021c902 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -1373,12 +1373,32 @@ _default_nonzero(void *ip, void *arr)
return FALSE;
}
+/*
+ Given a string return the type-number for
+ the data-type with that string as the type-object name.
+ Returns PyArray_NOTYPE without setting an error if no type can be
+ found. Only works for user-defined data-types.
+*/
+
+/*MULTIARRAY_API
+ */
+static int
+PyArray_TypeNumFromName(char *str)
+{
+ int i;
+ PyArray_Descr *descr;
+
+ for (i=0; i<PyArray_NUMUSERTYPES; i++) {
+ descr = userdescrs[i];
+ if (strcmp(descr->typeobj->tp_name, str) == 0)
+ return descr->type_num;
+ }
+
+ return PyArray_NOTYPE;
+}
/*
returns typenum to associate with this type >=PyArray_USERDEF.
- Also creates a copy of the VOID_DESCR table inserting it's typeobject in
- and it's typenum in the appropriate place.
-
needs the userdecrs table and PyArray_NUMUSER variables
defined in arratypes.inc
*/