I want to optimize.
connection = get_db_connection()
for item in my_iterator:
push_item_to_db(item, connection)
Drawback:
get_db_connection()
is slow. If my_iterator
is empty, then I want to avoid to call it.
connection = None
for item in my_iterator:
if connection is None:
connection = get_db_connection()
push_item_to_db(item, connection)
Drawback:
If there are 100k items in my_iterator
, then if connection is None
gets called 100k times (although it is needed only once). I want to avoid this.
get_db_connection()
if iterator is emptyif connection is None:
uselessly for every iteration.Any idea?
you can simply put the 'quit' outside the while loop since the while loop will end when the url is the back to the first page again. You can add an init flag to not break the while loop when you are in it with the url for the first time.
Yes, it should, every time, since lists are ordered. If you are iterating over a dict , the order may be different than expected.
Python for loop index start at 0 In Python by default, the range starts from 0 and ends at the value of the passed argument as -1. In this example, we do not iterate items through the list.
1. Most loops starts with 0.
You can do something like:
connection = None
for item in my_iterator:
if connection is None:
connection = get_db_connection()
push_item_to_db(item, connection)
Simple solution. Don't need to overthink it. Even with 100k operations, x is None
is just a reference comparison taking one Python opcode. You really don't need to optimise this compared to a full tcp roundtrip + disk write that happens on every insert.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With