mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
[Python] refactor slices on sorted (#86995)
Sometimes you want to query the small element of a set of elements and use `sorted(elements)[0]` without a second thought. However, this is not optimal, since the entire list must be sorted first `O(n log n)`. It would be better to use the `min(elements)` method provided for this purpose `O(n)`. Furthermore `sorted(elements)[::-1]` is not very efficient, because it would be better to use `sorted(elements, reverse=True)` to save the slice operation. **TLDR: using `sorted(elements)[0]` is slow and can be replaced with `min(elements)`.** I stumbled across these code snippets while playing around with CodeQL (see https://lgtm.com/query/4148064474379348546/). Pull Request resolved: https://github.com/pytorch/pytorch/pull/86995 Approved by: https://github.com/jansel
This commit is contained in:
committed by
PyTorch MergeBot
parent
98f40af7e3
commit
fd60b818b9
@ -191,7 +191,7 @@ def _all_gather(obj, worker_names=None, timeout=UNSET_RPC_TIMEOUT):
|
||||
_ALL_WORKER_NAMES is not None
|
||||
), "`_ALL_WORKER_NAMES` is not initialized for `def _all_gather`."
|
||||
worker_names = _ALL_WORKER_NAMES
|
||||
leader_name = sorted(worker_names)[0]
|
||||
leader_name = min(worker_names)
|
||||
|
||||
self_name = _get_current_rpc_agent().get_worker_info().name
|
||||
|
||||
|
Reference in New Issue
Block a user