# PyCall: Weirdish PyArray conversion performance behaviour

**URL:** <https://discourse.julialang.org/t/pycall-weirdish-pyarray-conversion-performance-behaviour/8454>\
**Category:** Performance\
**Created:** [January 18, 2018, 12:35pm UTC](https://discourse.julialang.org/t/pycall-weirdish-pyarray-conversion-performance-behaviour/8454 "2018-01-18T12:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [January 18, 2018, 12:35pm UTC](https://discourse.julialang.org/t/pycall-weirdish-pyarray-conversion-performance-behaviour/8454/1 "2018-01-18T12:35:47Z")

</div>

Hello,

I have a PyCall-wrapped python module (musdb) that natively gives me `PyArray{Float64}`, which can be fairly large.

At some stage I need an Array{Float32} of this, but conversion times vary a lot:

```julia
@elapsed convert(Array{Float32}, pa) ## 15.8
@elapsed convert(Array{Float32}, convert(Array{Float64}, pa)) ## 3.1
@elapsed convert(Array{Float32}, view(pa, :, :)) ## 0.063

```

`PyArray` is a view to the underlying python object, but apparently an explicit `view()` around it makes it more performant.

—david

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 18, 2018, 1:30pm UTC](https://discourse.julialang.org/t/pycall-weirdish-pyarray-conversion-performance-behaviour/8454/2 "2018-01-18T13:30:33Z")

</div>

> [@davidavdav](#):
>
> PyArray is a view to the underlying python object, but apparently an explicit view() around it makes it more performant.

The `PyArray` type was implemented a fairly long time ago, before all of the `IndexStyle` stuff in Base; it could be that the `convert` routine is somehow using linear indexing with `PyArray`, which will be slow since it uses `ind2sub`, rather than the newer `CartesianIndex` loops that are used by `SubArray`?

It would be interesting to drill down (with `@which` or `@edit`) to find what methods are being called by the `convert` routine, and what additional `IndexStyle` (or whatever) methods could be defined for `PyArray` to make it switch over to the faster path apparently used by `SubArray`. A PR would be welcome.

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [January 19, 2018, 2:48pm UTC](https://discourse.julialang.org/t/pycall-weirdish-pyarray-conversion-performance-behaviour/8454/3 "2018-01-19T14:48:21Z")

</div>

OK, thanks, I might have a look into this, but this would require quite a bit of study on my side.
