From 36720aa9615ebe4a7cf055ba79060faa2d5571a7 Mon Sep 17 00:00:00 2001 From: chillib Date: Fri, 25 Jul 2014 10:51:44 +0200 Subject: [PATCH] Example changed to use list comprehension for scalar multiplication. More equivalent to what numpy example does, and more pythonic (esp. since indices are not used/needed in calculation) --- python_examples/numpy_21_ex1.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python_examples/numpy_21_ex1.py b/python_examples/numpy_21_ex1.py index c0e20b4..82ed3bf 100644 --- a/python_examples/numpy_21_ex1.py +++ b/python_examples/numpy_21_ex1.py @@ -27,7 +27,16 @@ def list_times(alist, scalar): time1 = timeit.timeit('arr * 1.1', 'from __main__ import arr', number=N) / N print time1 +# More closely equivalent to what is being done in example above: +# return new array where all values are multiplied by scalar, leaving orignal unchanged +time2a = timeit.timeit('[a * 1.1 for a in larr]', + 'from __main__ import larr', number=N) / N +print time2a + # List and custom function for broadcasting time2 = timeit.timeit('list_times(larr, 1.1)', 'from __main__ import list_times, larr', number=N) / N -print time2 \ No newline at end of file +print time2 + + +print time2a/time1, time2/time1, "times slower"