If I want to replace a tensor x partially by y based on a condition (a Boolean tensor), I think the most suitable method is to call masked_scatter since it is designed for such usage. However, I found torch.where is even faster?
import torch
import cProfile

n=100000
dim=2000
device='cuda'

# masked_scatter_
x = torch.rand(n, dim, device=device)
y =  torch.rand(n, dim, device=device)
condition = torch.rand(n, dim, device=device) > 0.5
cProfile.run('x.masked_scatter_(condition, y)')


# torch.where
x = torch.rand(n, dim, device=device)
y =  torch.rand(n, dim, device=device)
condition = torch.rand(n, dim, device=device) > 0.5
cProfile.run('x = torch.where(condition, x, y)')

What I got is
 4 function calls in 0.112 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.112    0.112 <string>:1(<module>)
        1    0.000    0.000    0.112    0.112 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.112    0.112    0.112    0.112 {method 'masked_scatter_' of 'torch._C._TensorBase' objects}


4 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {built-in method where}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

Using CPU has a similar result. Besides, torch.where has a lower memory usage (not shown in the result above) despite I am using a inplace masked_scatter_. Why would this happen?