Hi, I am experiencing that the 
[sample](https://nim-lang.org/docs/random.html#sample%2CopenArray%5BT%5D) 
function is more than two times slower than manually indexing into rand(size-1):
    
    
    import sequtils
    import random
    import times
    
    
    proc main() =
        const
            size = 10_000_000
            times = 100_000_000
        let
            xs = newSeqWith(size, rand(0..10))
        
        var start = cpuTime()
        for i in 0..<times:
            let x = xs.sample
        echo "time taken sample: ", cpuTime() - start
        
        start = cpuTime()
        for i in 0..<times:
            let x = xs[rand(size - 1)]
        echo "time taken rand: ", cpuTime() - start
    
    
    main()
    
    
    Run

Compiling with -d=danger on my PC:

time taken sample: 4.956046000000001

time taken rand: 1.924525

Looking at the implementation I can't see how that would make sense, other than 
it being generic. Any ideas on why or if this is expected?

Reply via email to