Newbie Programming Mistakes: Difference between revisions
(Created page with "==Weird Syntax== ===comparison to true or false=== If you have a bool, you can just use it instead of comparing to <code>true</code> or <code>false</code>: <syntaxhighlight lang="python"> # some code which produces a bool b b = a > 5 # return b == false return not b </syntaxhighlight> Similarly, you don't need to do: <syntaxhighlight lang="python"> # Unnecessary if not b: return true else: return false </syntaxhighlight> ==Not using vectorization== If you're using...") |
|||
Line 19: | Line 19: | ||
==Not using vectorization== | ==Not using vectorization== | ||
If you're using a language like Python or R, you can take advantage of faster SIMD instructions by using vectorized code. | If you're using a language like Python or R, you can take advantage of faster SIMD instructions by using vectorized code.<br> | ||
Instead of writing: | Instead of writing: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> |
Revision as of 21:08, 10 June 2022
Weird Syntax
comparison to true or false
If you have a bool, you can just use it instead of comparing to true
or false
:
# some code which produces a bool b
b = a > 5
# return b == false
return not b
Similarly, you don't need to do:
# Unnecessary
if not b:
return true
else:
return false
Not using vectorization
If you're using a language like Python or R, you can take advantage of faster SIMD instructions by using vectorized code.
Instead of writing:
a = np.arange(5)
b = np.arange(5)
c = np.zeros(5)
# Bad
for i in range(len(a)):
c[i] = a[i] + 5*b[i]
# Good
c = a + 5*b