dharmik
@dharmik@linuxusers.in
61 following 21 followers
1. first test: sum of squares up to n (n = 10^6)
results:
using for-loop:the generator was slightly slower than the for-loop, which was surprising, since i expected the generator to be faster.
time taken: 0.271047 seconds
memory used (result): 32 bytesusing generator:
time taken: 0.278367 seconds
memory used (result): n/a
2. increased complexity test:
next, i made the test more complex by adding conditional logic (square even numbers, double odd ones) and using dictionaries instead of lists. the results were something like:
Using for-loop:yes, now the generator was faster, though slightly. it appears the change is only there when the dataset is bigger. #python
Time taken: 0.998669 seconds
Memory used (result): 32 bytesUsing generator:
Time taken: 0.990227 seconds
Memory used (result): N/A
@dharmik For an eye-opening deep dive on comparing generators which I have for many years recommended to *everyone* learning Python, see David Beazley's classic talk "Generator Tricks for Systems Programmers". The slides linked here stand completely alone. https://speakerdeck.com/dabeaz/generator-tricks-for-systems-programmers-version-3-dot-0