[Tutor] fast sampling with replacement
rabidpoobear at gmail.com
Sat Feb 20 19:58:26 CET 2010
Can you explain what your function is doing and also post some test code to
On Sat, Feb 20, 2010 at 10:22 AM, Andrew Fithian <afith13 at gmail.com> wrote:
> Hi tutor,
> I'm have a statistical bootstrapping script that is bottlenecking on a
> python function sample_with_replacement(). I wrote this function myself
> because I couldn't find a similar function in python's random library. This
> is the fastest version of the function I could come up with (I used
> cProfile.run() to time every version I wrote) but it's not fast enough, can
> you help me speed it up even more?
> import random
> def sample_with_replacement(list):
> l = len(list) # the sample needs to be as long as list
> r = xrange(l)
> _random = random.random
> return [list[int(_random()*l)] for i in r] # using
> list[int(_random()*l)] is faster than random.choice(list)
> FWIW, my bootstrapping script is spending roughly half of the run time in
> sample_with_replacement() much more than any other function or method.
> Thanks in advance for any advice you can give me.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor