-
-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explanation of speed difference between random_walk
and random_walk_faster
looks misleading
#101
Comments
Yes, when I say we get rid of the loop, I meant that the loop has been externalized at the C level (hopefully) and this is supposed to be faster because we do not have all the Python machinery for loop. To have a fair comparion, we should used the exact same random generator in two cases:
|
When I said "hidden in the machinery of Python" I meant vanilla Python code, but that comes from the standard library (not necessarily implemented in C). Most of the difference in speed execution (7 times faster vs 1.6 faster) between both functions is just because we're using two different ways of computing the next random step: Sorry if all this looks nitpicking. The book is great! |
Thank and no problem with nitpicking. When comparing the exact same method with an without loop, I still find the 7x factor:
|
If you see how
That's obviously faster. But C doesn't play much role here. It's just Python doing more (unnecessary) stuff in one case vs Python doing less stuff in the another. But OK, I think at this point I made myself clear enough. |
I was reading the Introduction of the book and I found myself trying to understand why
random_walk_faster
is ~7 times faster thanrandom_walk
. That's a huge difference!Those are the functions:
Running some tests, I figure out that most of the difference in speed is related to the way we compute the next random step, not because we are using a "vectorized approach" instead of a procedural, as stated in the explanation:
This last statement is also not accurate. Actually, we are just replacing an explicit loop for other loops hidden in the machinery of Python (that can, of course, rely on faster functions implemented in C).
Looking at the implementation of
random.choices
, the trick to gain speed is to use something likepopulation[floor(random.random() * len(population)]
to compute each next step.Applying this in the
random_walk
, we have:Now the difference of speed for 10k steps is ~1.6.
The text was updated successfully, but these errors were encountered: