Skip to content

random sampling from an (abstract)array is slow  #20582

@CarloLucibello

Description

@CarloLucibello

It is three times slower than this alternative implementation

julia> myrand(v) = (i = ceil(Int,rand()*length(v));  v[i])

julia> @benchmark rand(1:100)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     36.048 ns (0.00% GC)
  median time:      36.996 ns (0.00% GC)
  mean time:        37.104 ns (0.00% GC)
  maximum time:     66.210 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     993
  time tolerance:   5.00%
  memory tolerance: 1.00%

julia> @benchmark myrand(1:100)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     11.824 ns (0.00% GC)
  median time:      12.332 ns (0.00% GC)
  mean time:        12.286 ns (0.00% GC)
  maximum time:     35.627 ns (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     999
  time tolerance:   5.00%
  memory tolerance: 1.00%

Metadata

Metadata

Assignees

No one assigned

    Labels

    collectionsData structures holding multiple items, e.g. setsperformanceMust go fasterpotential benchmarkCould make a good benchmark in BaseBenchmarksrandomnessRandom number generation and the Random stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions