Currently I'm trying with my hobby 2 years of hobby scripting in python to optimize my old codes.
For a script in the past I had a check for giving out resources to a player. The whole calculation for 100.000 did take about 25 minutes. This has been shortened to a mere 1 minute and 40 seconds with a filter that if resources are equal to maximum storage it will not get the row from the table.
There are still checks in place in case one of te 3 resource types are full (so other resources will receive their production bonus.
Production is split between 4 since it will run once in 15 minutes.
Old code was a 25 minute run time. The code displayed here beneath does run over 100.000 "villages" within 1 minute and 40 seconds. 2,3 seconds to withdraw all data 0,2 seconds to write all data to database the rest (1 minute and 37,5 seconds) is purely what runs between
for village in villages: and session.add(add_resources)
Is it possible to speed this up even further or are these the limits of Python itself?
time_start, villages = datetime.utcnow(), new_get_villages()
for village in villages:
production_wood, production_stone, production_iron = int(village.wood_production/4), int(village.stone_production/4), int(village.iron_production/4)
add_resources = session.query(VillageNew).filter(VillageNew.pk == village.pk).first()
if add_resources.wood_stock != add_resources.max_storage:
add_resources.wood_stock = add_resources.wood_stock + production_wood
if add_resources.wood_stock > add_resources.max_storage:
add_resources.wood_stock = add_resources.max_storage
if add_resources.stone_stock != add_resources.max_storage:
add_resources.stone_stock = add_resources.stone_stock + production_stone
if add_resources.stone_stock > add_resources.max_storage:
add_resources.stone_stock = add_resources.max_storage
if add_resources.iron_stock != add_resources.max_storage:
add_resources.iron_stock = add_resources.iron_stock + production_iron
if add_resources.iron_stock > add_resources.max_storage:
add_resources.iron_stock = add_resources.max_storage
session.add(add_resources)
session.commit
time_end = datetime.utcnow()
session:
db_connection_string = conf.get_string('DBConf', 'db_connection_string')
engine = create_engine(db_connection_string, encoding='utf8')
Session = sessionmaker(bind=engine)
Code update after the post of @gimix
for village in session.query(VillageNew).filter(or_(VillageNew.wood_stock != VillageNew.max_storage, VillageNew.stone_stock != VillageNew.max_storage, VillageNew.iron_stock != VillageNew.max_storage)).all():
village.wood_stock = (village.wood_stock + int(village.wood_production))
if village.wood_stock > village.max_storage:
village.wood_stock = village.max_storage
village.stone_stock = (village.stone_stock + int(village.wood_production))
if village.stone_stock > village.max_storage:
village.stone_stock = village.max_storage
village.iron_stock = (village.iron_stock + int(village.wood_production))
if village.iron_stock > village.max_storage:
village.iron_stock = village.max_storage
session.commit()
Code update 02/11/2021:
session.query(VillageNew).where(VillageNew.wood_stock < VillageNew.max_storage).update({VillageNew.wood_stock: VillageNew.wood_stock + VillageNew.wood_production})
session.query(VillageNew).where(VillageNew.stone_stock < VillageNew.max_storage).update({VillageNew.stone_stock: VillageNew.stone_stock + VillageNew.stone_production})
session.query(VillageNew).where(VillageNew.iron_stock < VillageNew.max_storage).update({VillageNew.iron_stock: VillageNew.iron_stock + VillageNew.iron_production})
session.query(VillageNew).where(VillageNew.wood_stock > VillageNew.max_storage).update({VillageNew.wood_stock: VillageNew.max_storage})
session.query(VillageNew).where(VillageNew.stone_stock > VillageNew.max_storage).update({VillageNew.stone_stock: VillageNew.max_storage})
session.query(VillageNew).where(VillageNew.iron_stock > VillageNew.max_storage).update({VillageNew.iron_stock: VillageNew.max_storage})