"""Very simple test that compares the speed of operating with aligned vs unaligned arrays. """ from timeit import Timer import numpy as np import numexpr as ne niter = 10 # shape = (1000*10000) # unidimensional test shape = (1000, 10000) # multidimensional test ne.print_versions() Z_fast = np.zeros(shape, dtype=[("x", np.float64), ("y", np.int64)]) Z_slow = np.zeros(shape, dtype=[("x", np.float64), ("y", np.bool)]) x_fast = Z_fast["x"] t = Timer("x_fast * x_fast", "from __main__ import x_fast") print "NumPy aligned: \t", round(min(t.repeat(3, niter)), 3), "s" x_slow = Z_slow["x"] t = Timer("x_slow * x_slow", "from __main__ import x_slow") print "NumPy unaligned:\t", round(min(t.repeat(3, niter)), 3), "s" t = Timer("ne.evaluate('x_fast * x_fast')", "from __main__ import ne, x_fast") print "Numexpr aligned:\t", round(min(t.repeat(3, niter)), 3), "s" t = Timer("ne.evaluate('x_slow * x_slow')", "from __main__ import ne, x_slow") print "Numexpr unaligned:\t", round(min(t.repeat(3, niter)), 3), "s"
expr9 = 'where(a%2 != 0.0, 2, b+5)' expr10 = 'a**2 + (b+1)**-2.5' expr11 = '(a+1)**50' expr12 = 'sqrt(a**2 + b**2)' def compare(check_only=False): experiments = [(setup1, expr1), (setup2, expr2), (setup3, expr3), (setup4, expr4), (setup5, expr5), (setup5, expr6), (setup5, expr7), (setup5, expr8), (setup5, expr9), (setup5, expr10), (setup5, expr11), (setup5, expr12), ] total = 0 for params in experiments: total += compare_times(*params) print average = total / len(experiments) print "Average =", round(average, 2) return average if __name__ == '__main__': import numexpr numexpr.print_versions() averages = [] for i in range(iterations): averages.append(compare()) print "Averages:", ', '.join("%.2f" % x for x in averages)
(setup1, expr1), (setup2, expr2), (setup3, expr3), (setup4, expr4), (setup5, expr5), (setup5, expr6), (setup5, expr7), (setup5, expr8), (setup5, expr9), (setup5, expr10), (setup5, expr11), (setup5, expr12), ] total = 0 for params in experiments: total += compare_times(*params) print average = total / len(experiments) print "Average =", round(average, 2) return average if __name__ == '__main__': import numexpr numexpr.print_versions() averages = [] for i in range(iterations): averages.append(compare()) print "Averages:", ', '.join("%.2f" % x for x in averages)
# See LICENSE.txt and LICENSES/*.txt for details about copyright and # rights to use. #################################################################### """Very simple test that compares the speed of operating with aligned vs unaligned arrays. """ from timeit import Timer import numpy as np import numexpr as ne niter = 10 #shape = (1000*10000) # unidimensional test shape = (1000, 10000) # multidimensional test ne.print_versions() Z_fast = np.zeros(shape, dtype=[('x', np.float64), ('y', np.int64)]) Z_slow = np.zeros(shape, dtype=[('y1', np.int8), ('x', np.float64), ('y2', np.int8, (7, ))]) x_fast = Z_fast['x'] t = Timer("x_fast * x_fast", "from __main__ import x_fast") print "NumPy aligned: \t", round(min(t.repeat(3, niter)), 3), "s" x_slow = Z_slow['x'] t = Timer("x_slow * x_slow", "from __main__ import x_slow") print "NumPy unaligned:\t", round(min(t.repeat(3, niter)), 3), "s" t = Timer("ne.evaluate('x_fast * x_fast')", "from __main__ import ne, x_fast")