-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
[keras/benchmarks/benchmark_util.py] Use var rather than string literal for is None
checks on measure_performance
#17980
[keras/benchmarks/benchmark_util.py] Use var rather than string literal for is None
checks on measure_performance
#17980
Conversation
…al for `is None` checks on `measure_performance`
raise ValueError("Input data is required.") | ||
if "optimizer" is None: | ||
elif optimizer is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @SamuelMarks,
Just curiosity, why did you switch into elif
instead of if
? I think keeping them independent might be a better practice, i.e maintain the if
statements.
P.S: Don't take this as an official-review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Frightera Because it's more efficient:
$ python -m timeit 'a="foo"' 'if a == "foo":' ' a += "bar"' 'if a == "can":' ' a += "can"'
5000000 loops, best of 5: 51.9 nsec per loop
$ python -m timeit 'a="foo"' 'if a == "foo":' ' a += "bar"' 'elif a == "can":' ' a += "can"'
10000000 loops, best of 5: 39.6 nsec per loop
Also it's more logical. The compiler probably optimises the branches better, and it's clearer to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I think the speed difference is because of every if
statement is executed in your example whereas in here there are ValueError
s which will cause not to execute other statements, probably the performance difference will be negligible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
…tring literal for `is None` checks on `measure_performance` Imported from GitHub PR #17980 Found by running `pydocstyle` on entire codebase. Copybara import of the project: -- f7de756 by Samuel Marks <[email protected]>: [keras/benchmarks/benchmark_util.py] Use var rather than string literal for `is None` checks on `measure_performance` Merging this change closes #17980 FUTURE_COPYBARA_INTEGRATE_REVIEW=#17980 from SamuelMarks:benchmark_util-is-check f7de756 PiperOrigin-RevId: 526075166
…tring literal for `is None` checks on `measure_performance` Imported from GitHub PR #17980 Found by running `pydocstyle` on entire codebase. Copybara import of the project: -- f7de756 by Samuel Marks <[email protected]>: [keras/benchmarks/benchmark_util.py] Use var rather than string literal for `is None` checks on `measure_performance` Merging this change closes #17980 FUTURE_COPYBARA_INTEGRATE_REVIEW=#17980 from SamuelMarks:benchmark_util-is-check f7de756 PiperOrigin-RevId: 526075166
…tring literal for `is None` checks on `measure_performance` Imported from GitHub PR #17980 Found by running `pydocstyle` on entire codebase. Copybara import of the project: -- f7de756 by Samuel Marks <[email protected]>: [keras/benchmarks/benchmark_util.py] Use var rather than string literal for `is None` checks on `measure_performance` Merging this change closes #17980 FUTURE_COPYBARA_INTEGRATE_REVIEW=#17980 from SamuelMarks:benchmark_util-is-check f7de756 PiperOrigin-RevId: 530510171
Found by running
pydocstyle
on entire codebase.