copyto! test

This page examines copyto! speed of the lazy grids in the Julia package LazyGrids.

This page comes from a single Julia file: 2-copyto.jl.

You can access the source code for such Julia documentation using the 'Edit on GitHub' link in the top right. You can view the corresponding notebook in nbviewer here: 2-copyto.ipynb, or open it in binder here: 2-copyto.ipynb.

Setup

Packages needed here.

using LazyGrids: ndgrid, ndgrid_array
using LazyGrids: btime, @timeo # not exported; just for timing tests here
using BenchmarkTools: @benchmark
using InteractiveUtils: versioninfo

Overview

There are several sub-types of AbstractGrids. Here we focus on the simplest (using OneTo) and the most general (AbstractVector).

OneTo

dims = (2^7,2^8,2^9)

(xl, _, _) = ndgrid(dims...) # lazy version
xa = Array(xl) # regular dense Array
out = zeros(Int, dims)
sizeof.((xl, xa))
(24, 134217728)
ta = @benchmark copyto!(out, xa) # 12.6ms
btime(ta)
"time=5.9ms mem=0 alloc=0"
tl = @benchmark copyto!(out, xl) # 27.3ms
btime(tl)
"time=21.6ms mem=0 alloc=0"

AbstractVector

x,y,z = map(rand, dims)

(xl, _, _) = ndgrid(dims...)
xa = Array(xl)
out = zeros(eltype(x), dims)
sizeof.((xl, xa))
(24, 134217728)
ta = @benchmark copyto!(out, xa) # 15.7ms
btime(ta)
"time=8.8ms mem=0 alloc=0"
tl = @benchmark copyto!(out, xl) # 21.7ms
btime(tl)
"time=21.6ms mem=0 alloc=0"

These results suggest that copyto! is somewhat slower for a lazy grid than for an Array. This drawback could be reduced or possibly even eliminated by adding a dedicated copyto! method for lazy grids. Submit an issue or PR if there is a use case that needs faster copyto!.

See broadcasting.

Reproducibility

This page was generated with the following version of Julia:

using InteractiveUtils: versioninfo
io = IOBuffer(); versioninfo(io); split(String(take!(io)), '\n')
12-element Vector{SubString{String}}:
 "Julia Version 1.10.3"
 "Commit 0b4590a5507 (2024-04-30 10:59 UTC)"
 "Build Info:"
 "  Official https://julialang.org/ release"
 "Platform Info:"
 "  OS: Linux (x86_64-linux-gnu)"
 "  CPU: 4 × AMD EPYC 7763 64-Core Processor"
 "  WORD_SIZE: 64"
 "  LIBM: libopenlibm"
 "  LLVM: libLLVM-15.0.7 (ORCJIT, znver3)"
 "Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)"
 ""

And with the following package versions

import Pkg; Pkg.status()
Status `~/work/LazyGrids.jl/LazyGrids.jl/docs/Project.toml`
  [6e4b80f9] BenchmarkTools v1.5.0
  [e30172f5] Documenter v1.4.1
  [7031d0ef] LazyGrids v1.0.0 `~/work/LazyGrids.jl/LazyGrids.jl`
  [98b081ad] Literate v2.18.0
  [b77e0a4c] InteractiveUtils

This page was generated using Literate.jl.