def convert(x, coerce): if isinstance(x, xnd.array): return x try: return xnd.array.from_buffer(memoryview(x)) except TypeError: pass if coerce: return xnd.array(x) return NotImplemented
def convert(x, coerce): if isinstance(x, xnd.array): return x try: return xnd.array.from_buffer(memoryview(x)) except TypeError: pass if coerce: return xnd.array(x) if isinstance(x, (int, float, bool)): return x raise ua.BackendNotImplementedError("Unsupported output received.")
# Python data conversion # ======================================================================== # # Timeit is too slow for large arrays, so this benchmark just uses time.time(), # which is accurate enough for this purpose. # ### List of structured dtypes, from the numpy documentation ### lst = [('Rex', 9, 81.0)] * 10000000 # Infer the type. This is only possible in xnd, NumPy infers a Python object type. print("\nType inference") print("---------------\n") start = time() x = xnd.array(lst) stop = time() print("xnd: ", stop - start) # Dtype is provided. print("\nDtype provided") print("--------------\n") start = time() x = xnd.array(lst, dtype="(fixed_string(10), int32, float32)") stop = time() print("xnd: ", stop - start) del x dt = np.dtype([('name', 'U10'), ('age', 'i4'), ('weight', 'f4')]) start = time() x = np.array(lst, dtype=dt)
import xnd import numpy as np from time import time repeat = 1000000 ### Small array view ### lst = [[1,2,3], [3,4,5]] print("\nSmall subarray view") print("-------------------\n") x = xnd.array(lst) start = time() for i in range(repeat): y = x[1] stop = time() print(" xnd: ", stop-start) del x, y start = time() x = np.array(lst) for i in range(repeat): y = x[1] stop = time() print(" numpy:", stop-start) del x, y ### Medium sized array view ### print("\nMedium sized subarray view")
# Python data conversion # ======================================================================== # # Timeit is too slow for large arrays, so this benchmark just uses time.time(), # which is accurate enough for this purpose. # ### List of int64_t values ### lst = [1] * 10000000 # Type inference for reading a medium sized Python list. print("\nType inference") print("--------------\n") start = time() x = xnd.array(lst) stop = time() print(" xnd: ", stop - start) del x start = time() x = np.array(lst) stop = time() print(" numpy:", stop - start) del x # The same, but with an explicit dtype. print("\nDtype provided") print("--------------\n") start = time() x = xnd.array(lst, dtype="int64")