I have a Python program whose results will be saved in a big numpy array. I want to use this result in my Julia program, but I find that accessing the data will cause memory allocation in Julia. A demo is as follows:
using TimerOutputs
using PyCall
py"""
import numpy as np
def funa():
return np.random.rand(36,36)
"""
function test_copy(B)
@timeit "copy" B .= py"""funa()"""
end
B = zeros(36,36)
test_copy(B)
reset_timer!()
test_copy(B)
show(TimerOutputs.get_defaulttimer());
The output is:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Time Allocations
βββββββββββββββββββββββ ββββββββββββββββββββββββ
Tot / % measured: 191ΞΌs / 13.7% 16.1KiB / 71.7%
Section ncalls time %tot avg alloc %tot avg
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
copy 1 26.2ΞΌs 100.0% 26.2ΞΌs 11.6KiB 100.0% 11.6KiB
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
What I want to do is, since a numpy array has already been created in Python, how can I directly copy the data to Juliaβs array A
without causing memory allocation?
Thanks.