Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example changed to use list comprehension for scalar multiplication. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion python_examples/numpy_21_ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
print time2


print time2a/time1, time2/time1, "times slower"