You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to optimize reading long tuples from julia to python #575
Is your feature request related to a problem? Please describe.
This code takes around 7s on a modern i9 the first time that it's executed. It's immediate the next times it's executed. It's also immediate to execute (zeros(20_000)...,) in julia.
I have python 3.12 and julia 1.10.5
Describe the solution you'd like
Is there any way to speed up the initial compilation?
Describe alternatives you've considered
I tried to use this code to to speed up reading data from julia to python by going through a tuple. Looping a long julia vector from python is much slower than looping a tuple ( around 4 times slower to read a 100_000 floats vector vs reading a tuple)
I can't explain the speed difference (seval is directly calling Julia to execute that code, there is essentially zero overhead from JuliaCall) but you shouldn't be creating tuples of thousands of elements in Julia anyway. What are you actually trying to do?
I tried to use this code to to speed up reading long vectors from julia to python by going. Looping a long julia vector from python is much slower than looping a tuple ( around 4 times slower to read a 100_000 floats vector vs reading a tuple). So I tried converting the vector to a tuple in julia before reading it. Indeed it's much faster, but the compilation overhead is huge.
An alternative I found is, instead of the reading the long julia vector from python, I pass a python vector to julia and julia populates it. This results in being around x3 faster also.
get_v what I'd like to optimize (reading a long julia vector). set_v is the workaround that, by passing a python array to julia, the reading of the vector becomes 3 times faster.
The slow part won't be the call to get_v but the summation you perform afterwards. This is because get_v is returning a lazy wrapper around the rand(n) array, so when you do the sum, each element is materialised lazily one by one by calling into Julia. In your case it will be faster to materialise the whole array as a Python list in one go, such as by defining get_v(v, n) = pylist(rand(n)).
Activity
cjdoris commentedon Nov 20, 2024
I can't explain the speed difference (
sevalis directly calling Julia to execute that code, there is essentially zero overhead from JuliaCall) but you shouldn't be creating tuples of thousands of elements in Julia anyway. What are you actually trying to do?dpinol commentedon Nov 20, 2024
I tried to use this code to to speed up reading long vectors from julia to python by going. Looping a long julia vector from python is much slower than looping a tuple ( around 4 times slower to read a 100_000 floats vector vs reading a tuple). So I tried converting the vector to a tuple in julia before reading it. Indeed it's much faster, but the compilation overhead is huge.
An alternative I found is, instead of the reading the long julia vector from python, I pass a python vector to julia and julia populates it. This results in being around x3 faster also.
cjdoris commentedon Nov 21, 2024
Can you give some code to demonstrate what you're doing, i.e. some code that works but is slow that you'd like to be faster?
dpinol commentedon Nov 27, 2024
get_vwhat I'd like to optimize (reading a long julia vector).set_vis the workaround that, by passing a python array to julia, the reading of the vector becomes 3 times faster.This is what I get on a modern i9:
cjdoris commentedon Apr 11, 2025
The slow part won't be the call to
get_vbut the summation you perform afterwards. This is becauseget_vis returning a lazy wrapper around therand(n)array, so when you do the sum, each element is materialised lazily one by one by calling into Julia. In your case it will be faster to materialise the whole array as a Python list in one go, such as by definingget_v(v, n) = pylist(rand(n)).dpinol commentedon Dec 10, 2025
@cjdoris pylist did indeed had a dramatic performance improvement. thanks!